Game Development Reference
In-Depth Information
arbitrary acceleration instead of just using gravity as the force (e.g., percent of jet
engine thrust).
kWorldHeight and kWorldWidth
These values are used to allow the programmer to change the assumed world di‐
mensions. Higher values mean each pixel is a greater distance in meters. The world
will always be scaled to fit on the screen, so a large world means the sprite will appear
to move slower (a few pixels at a time) for a given acceleration. Note that our current
code doesn't scale the sprite.
Now we'll show you how we use these variables in SpriteView.m to move our sprite on
our screen as a result of the accelerometer values. First, we have some initialization to
do, which takes place in the initWithCoder: method that runs the first time the view
is loaded:
-(id)initWithCoder:(NSCoder *)coder {
if((self = [super initWithCoder:coder])){
self.sprite = [UIImage imageNamed:@"sprite.png"];
self.currentPos = CGPointMake((self.bounds.size.width / 2.0f) +
(sprite.size.width / 2.0f), (self.bounds.size.height /2.0f)+(sprite.size.height /2.0f));
xVelocity = 0.0f;
yVelcoity = 0.0f;
convertX = self.bounds.size.width / kWorldWidth;
convertY = self.bounds.size.height / kWorldHeight;
return self;
Most of this is pretty straightforward. We tell our program where to find the sprite image
we've chosen and set its initial position to the center of the screen. We also set its initial
velocity to 0 in both directions. We then go ahead and initialize our convertX and
convertY variables based on the self.bounds.size property, which gives the bounds
of the view in pixels. We'll show exactly how this affects our program later. Next, we'll
write a custom mutator for the CurrentPos variable:
- (void)setCurrentPos:(CGPoint)newPos {
prevPos = currentPos;
currentPos = newPos;
if(currentPos.x <0){
currentPos.x = 0;
xVelocity = 0.0f;
if(currentPos.y <0){
currentPos.y = 0;
yVelcoity = 0.0f;
if(currentPos.x > self.bounds.size.width - sprite.size.width){
Search Nedrilad ::

Custom Search