Game Development Reference
the actual image size may be less than the texture size—for example, the case of the
image being 100 × 100 pixels while the texture has to be 128×128 pixels. The con-
tentSize property of the texture returns the original image's size of 100×100 pixels.
In most cases, you'll want to work with the content size, not the texture size. Even if
your image is a power of two, you should use contentSize because the texture
might be a texture atlas containing multiple images. Texture atlases are covered in
By taking half the image height and setting this as the position on the y-axis, the sprite
image will align neatly with the bottom of the screen.
Tip Avoiding using fixed positions wherever you can is good practice. If you
simply set the player position to 160,32, you're making two assumptions you
should avoid. First, you're assuming the screen width will be 320 pixels, but
that will not hold true for every iOS device. Second, you're assuming that the
image height is 64 pixels, but that might change too. Once you start to make as-
sumptions like these, your code will be less flexible, and it takes more time to
The way I wrote the positioning code involves a bit more typing, but in the
long run this pays off big time. You can deploy to different devices and it'll
work, and you can use different image sizes and it'll work. There's no need to
change this particular code anymore. One of the most time-consuming tasks a
programmer faces is having to change code that's based on assumptions.
Simple Accelerometer Input
One last step, and then you're done tilting the player sprite around. As I demonstrated
erometer input. Here I use the acceleration.x parameter and add it to the player's
position; multiplying by 10 is to speed up the player's movement.
-(void) accelerometer:(UIAccelerometer *)accelerometer
CGPoint pos = player.position;