Game Development Reference
In-Depth Information
player = [CCSprite spriteWithFile:@"alien.png"];
[self addChild:player z:0 tag:1];
CGSize screenSize = [CCDirector sharedDirector].winSize;
float imageHeight = player.texture.contentSize.height;
player.position = CGPointMake(screenSize.width / 2, imageHeight / 2);
return self;
The player sprite is added as a child with a tag of 1, which will later be used to idenjp-
gy and separate the player sprite from all other sprites. For the filename, the standard
resolution image filename is used, in this case alien.png . Cocos2d will automatic-
ally load the alien-hd.png on Retina devices. If there's no accompanying -hd file,
cocos2d will simply load the standard-resolution image instead. In that case the image
will appear smaller on Retina devices than it will be on non-Retina devices. It's good
practice to provide -hd variants of all image assets.
Caution Filenames on iOS devices are case-sensitive. If you try to load Ali-
en.png or ALIEN.PNG , it will work in the simulator but not on any iOS
device because the real name is alien.png in all lowercase. That's why it's a
good idea to stick to a naming convention like consistently keeping all file-
names in all lowercase. Why lowercase? Because filenames in all uppercase are
typically harder to read, and they speak to you quite loudly.
You set the initial position of the player sprite by centering the x position at half the
screen width, which puts the sprite in the center horizontally. Vertically you want the
bottom of the player sprite's texture to align with the bottom of the screen. If you re-
member from Chapter 3 , you know that the sprite texture is centered on the node's pos-
ition. Positioning the sprite vertically at 0 would cause the bottom half of the sprite tex-
ture to be below the screen. That's not what you want—you want to move it up by half
the texture height.
You do that with the call to player.texture.contentSize.height , which
returns the sprite texture's content size. What exactly is the content size? In Chapter 3 ,
I mentioned that the texture dimensions of iOS devices can only be powers of two. But
Search Nedrilad ::

Custom Search