Here is my situation:
I have followed a simple guide from the book 'Beginning iPhone Development' but would like to tweak one major aspect (I have tweaked a few things: adding a 'response' when the sprite hits the wall instead of just stopping the velocity, sped up the sprite, etc. = basic tweaks). What I want to do is use the sprite's animation (based off the accelerometer's orienation) on top of another ImageView that will be playng an animation in the background. The way the code is currently set up just loads everything without any user interaction. I would like the sprite, etc. to only show up after an action is called. Preferably, I would like to keep all the code within the same class as well, making the exit simpler as well. Thanks in advance: here is my current code:
BallViewController.h
BallViewController.m
BallView.h
BallView.m
*All this code works on it's own, but I want to use it on top of a imageView within the first class (that code isn't included). I have all that stuff figured out easily and pretty much understand this code, but the pragma mark is tripping me up a little bit, as well as some of the calls from the first class. Once again, thanks in advance.
I have followed a simple guide from the book 'Beginning iPhone Development' but would like to tweak one major aspect (I have tweaked a few things: adding a 'response' when the sprite hits the wall instead of just stopping the velocity, sped up the sprite, etc. = basic tweaks). What I want to do is use the sprite's animation (based off the accelerometer's orienation) on top of another ImageView that will be playng an animation in the background. The way the code is currently set up just loads everything without any user interaction. I would like the sprite, etc. to only show up after an action is called. Preferably, I would like to keep all the code within the same class as well, making the exit simpler as well. Thanks in advance: here is my current code:
BallViewController.h
Code:
#define kUpdateInterval (1.0f/60.0f)
#import <UIKit/UIKit.h>
@interface BallViewController : UIViewController <UIAccelerometerDelegate> {
}
@end
Code:
#import "BallViewController.h"
#import "BallView.h"
@implementation BallViewController
- (void)viewDidLoad {
UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
accelerometer.delegate = self;
accelerometer.updateInterval = kUpdateInterval;
[super viewDidLoad];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#pragma mark -
- (void)accelerometer:(UIAccelerometer *)accelerometer
didAccelerate:(UIAcceleration *)acceleration {
[(BallView *)self.view setAcceleration:acceleration];
[(BallView *)self.view draw];
}
@end
Code:
#define kVelocityMultiplier 1000
#import <UIKit/UIKit.h>
@interface BallView : UIView {
UIImage *image;
CGPoint currentPoint;
CGPoint previousPoint;
UIAcceleration *acceleration;
CGFloat ballXVelocity;
CGFloat ballYVelocity;
}
@property (nonatomic, retain) UIImage *image;
@property CGPoint currentPoint;
@property CGPoint previousPoint;
@property (nonatomic, retain) UIAcceleration *acceleration;
@property CGFloat ballXVelocity;
@property CGFloat ballYVelocity;
-(void)draw;
@end
Code:
#import "BallView.h"
@implementation BallView
@synthesize image;
@synthesize currentPoint;
@synthesize previousPoint;
@synthesize acceleration;
@synthesize ballXVelocity;
@synthesize ballYVelocity;
-(id)initWithCoder:(NSCoder *)coder {
if (self = [super initWithCoder:coder]) {
self.image = [UIImage imageNamed:@"TealBall.png"];
self.currentPoint = CGPointMake((self.bounds.size.width / 2.0f) + (image.size.width /2.0f), (self.bounds.size.height / 2.0f) + (image.size.height / 2.0f));
ballXVelocity = 0.0f;
ballYVelocity = 0.0f;
}
return self;
}
-(id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
//Initialization code
}
return self;
}
-(void)drawRect:(CGRect)rect {
[image drawAtPoint:currentPoint];
}
-(CGPoint)currentPoint {
return currentPoint;
}
-(void)setCurrentPoint:(CGPoint)newPoint {
previousPoint = currentPoint;
currentPoint = newPoint;
if (currentPoint.x < 0) {
currentPoint.x = 0;
ballXVelocity = - (ballXVelocity *.75);
}
if (currentPoint. y < 0) {
currentPoint.y = 0;
ballYVelocity = - (ballYVelocity *.75);
}
if (currentPoint.x > self.bounds.size.width - image.size.width) {
currentPoint.x = self.bounds.size.width - image.size.width;
ballXVelocity = - (ballXVelocity *.75);
}
if (currentPoint.y > self.bounds.size.height - image.size.height) {
currentPoint.y = self.bounds.size.height - image.size.height;
ballYVelocity = - (ballYVelocity *.75);
}
CGRect currentImageRect = CGRectMake(currentPoint.x, currentPoint.y, currentPoint.x + image.size.width, currentPoint.y + image.size.height);
CGRect previousImageRect = CGRectMake(previousPoint.x, previousPoint.y, previousPoint.x + image.size.width, currentPoint.y + image.size.width);
[self setNeedsDisplayInRect:CGRectUnion(currentImageRect, previousImageRect)];
}
-(void)draw {
static NSDate *lastDrawTime;
if (lastDrawTime != nil) {
NSTimeInterval secondsSinceLastDraw =
-([lastDrawTime timeIntervalSinceNow]);
ballYVelocity = ballYVelocity + -(acceleration.y * secondsSinceLastDraw);
ballXVelocity = ballXVelocity + acceleration.x * secondsSinceLastDraw;
CGFloat xAcceleration = secondsSinceLastDraw *ballXVelocity * 1000;
CGFloat yAcceleration = secondsSinceLastDraw *ballYVelocity * 1000;
self.currentPoint = CGPointMake(self.currentPoint.x + xAcceleration, self.currentPoint.y +yAcceleration);
}
[lastDrawTime release];
lastDrawTime = [[NSDate alloc] init];
}
- (void)dealloc {
[image release];
[acceleration release];
[super dealloc];
}
@end