Game Development Reference
In-Depth Information
scoreLabel.anchorPoint = CGPointMake(0.5f, 1.0f);
// Add the score label with z value of -1 so it's drawn below everything else
[self addChild:scoreLabel z:-1];
You also need to declare the score and scoreLabel instance variables in the
GameLayer.h file. Instead of adding a CCLabelTTF object, however, use a
CCNode object that implements the CCLabelProtocol . That way, later you can
change the implementation from CCLabelTTF to CCLabelBMFont without having
to change the instance variable's declaration. Using the protocol instead of a concrete
class is an example of interface-based programming, which makes programs easier to
modify and maintain. The scoreLabel is now oblivious to whether it's a CCLa-
belTTF or CCLabelBMFont object.
@interface GameLayer : CCLayer
{
CCSprite* player;
CGPoint playerVelocity;
NSMutableArray* spiders;
float spiderMoveDuration;
int numSpidersMoved;
int score;
CCNode < CCLabelProtocol > * scoreLabel;
}
I consciously chose a CCLabelTTF object for now because it would likely be the first
choice for most beginning cocos2d programmers. And that's where things might get
ugly pretty fast. In Chapter 3 , I mentioned that updating a CCLabelTTF 's text is slow.
The whole texture is re-created using iOS font-rendering methods, and they take their
time, besides allocating a new texture and releasing the old one. On current-generation
devices and with a single label, you probably won't notice it. But use several frequently
updating devices and run the game on an iPhone 3GS and perhaps you'll notice a
severe drop in framerate.
Search Nedrilad ::




Custom Search