Game Development Reference
In-Depth Information
self.scoreLabel.zPosition = 100;
[self addChild:self.scoreLabel];
SKAction *tempAction = [SKAction runBlock:^{
self.scoreLabel.text = [NSString
stringWithFormat:@"%3.0f", self.score];
}];
SKAction *waitAction = [SKAction waitForDuration:0.2];
[self.scoreLabel runAction:[SKAction
repeatActionForever:[SKAction sequence:@[tempAction, waitAction]]]];
Why would we need so many actions? We could go the easier way of refreshing
a label in the update: method, but rendering text on labels is an expensive task,
and calling it for every update is not a good idea. That's why we create an action
that updates the label every 0.2 seconds.
The label is created the way you expect; we set the properties that we need and add
it as a child to the scene.
Next, add the following code to the end of the update: method:
self.score = self.score + (backgroundMoveSpeed * timeSinceLast / 100);
Now the label updates in real time. As our character moves, the score increases.
Summary
In this chapter, we have learned the basics of Sprite Kit. There are many things that
have to be finished in our game, but we have a robust background now. Our project
is relatively small, but we can already see the basic gameplay elements in action.
Things that we have learned are:
• The properties and hierarchy of SKNodes
• SKSpriteNode, anchoring, and drawing
• How to draw an infinite scrolling background
How to draw a text label
How to move a sprite on screen (actions and update method)
How a game loop operates
In the next chapter, we will discuss interacting with our game, handling touches and
gesture recognizers, and moving sprites.
 
Search WWH ::




Custom Search