Game Development Reference
In-Depth Information
CCSprite , certainly the most commonly used class, uses an image to display the
sprite onscreen. The simplest way to create a sprite is from a file that's loaded into a
CCTexture2D texture and assigned to the sprite. You have to add the image file to
the Resources group in Xcode; otherwise, the app can't find the file:
CCSprite* sprite = [CCSprite spriteWithFile:@"Default.png"];
[self addChild:sprite];
Here's a question for you: where do you think this sprite will be positioned on the
screen? Contrary to what you might be used to from other game engines, the texture is
centered on the sprite's position. The sprite just initialized will be located at position
0,0, so it's positioned at the lower left-hand corner of the screen. Because the sprite's
texture is centered on the sprite's position, the texture is only partially visible. Assum-
ing the image is 80×30 pixels in size, you'd have to move the sprite to position 40,15
to make the texture align perfectly with the lower left-hand corner of the screen and be
fully visible.
Although unusual at first glance, centering the texture on the sprite does have great ad-
vantages. Once you start using the rotation or scale properties of the sprite, the sprite
will stay centered on its position.
Warning Filenames are case-sensitive on iOS devices. While you're on the Simulator,
the filename's case doesn't matter, but when you switch to testing on the device it will
most likely crash if the filename is actually something like @"default.PNG" (from
the previous example).
This sensitivity has caused many developers serious headaches, and it's another reason
you should test on the device often. Coming up with a naming scheme for filenames
and sticking to it is also a good idea. Personally, I keep them in lowercase, using dashes
where needed to separate words.
Anchor Points Demystified
Every node has an anchor point, but it only starts to make a difference if the node has a
texture, like CCSprite or CCLabelTTF . By default, the anchorPoint property is
at 0.5,0.5, or, in other words, at the center of the texture (texture width and height times
0.5).
The anchor point has nothing to do with the node's position, even though changing the
anchorPoint will change where the texture is rendered onscreen. By modifying the
Search Nedrilad ::




Custom Search