Game Development Reference
In-Depth Information
Having done this, we can set up a much more precise movement. Let's add a new
constant to the
Common.h
ile:
static NSInteger backgroundMoveSpeed = 30;
We will use this to handle background scrolling, new code to scroll background that
uses timing, and add it after handling time in the
update:
method:
[self enumerateChildNodesWithName:backgroundName
usingBlock:^(SKNode *node, BOOL *stop) {
node.position = CGPointMake(node.position.x -
backgroundMoveSpeed * timeSinceLast, node.position.y);
}];
We multiply time passed by speed to get the amount of pixels that the background
needs to be shifted in this frame (by frame, we mean the picture that gets drawn
60 times per second on screen, not the sprite bounding box). On the next frame,
the calculation proceeds, and if for some reason the frame rate drops, the user
won't notice it as the background moves at the same speed regardless of the frame
rate. There is a problem when the background scrolls too far to the left; we are left
with an empty screen. We will fix this in
Chapter 4
,
Animating Sprites
.
The next thing that we need to do is to add a character. Add the
character.png
image to the project in the same way as you did with the background (find
Images.
xcassets
, select it, drag-and-drop the file there). Don't forget to set this image as a
2x
image, since it is high resolution.
We will need a separate class for our player. First, let's add a new string to
Common.h
to identify the player node:
static NSString *playerName = @"player";
After this, create a new class called
ERGPlayer
and make it inherit from
SKSpriteNode
. Import the header file (
ERGPlayer
) into
ERGScene.m
so
that our program can access methods and properties from it.