Game Development Reference
In-Depth Information
How it works...
Implementation of infinite scrolling is divided into three parts. We need to follow the fol-
lowing steps to accomplish infinite scrolling for our game:
• Initializing the SpaceBackground: Two space backgrounds are added one after the
other so that they are moved at the same time to give a feel of an infinite scrolling
background.
• SpaceBackground move code: Here, a block method of SKScene is used to iterate
all nodes of the scene.
[self enumerateChildNodesWithName:@"SpaceBG"
usingBlock: ^(SKNode *node,
BOOL *stop)
{
}];
In this iteration, the SpaceBGNode is identified by its name so that its position can be up-
dated.
SKSpriteNode * spaceBGNode = (SKSpriteNode *) node;
The amount of distance to be moved is calculated using the CGPointMultiplyScalar
inline function that is fed with the constant value SPACE_BG_VELOCITY and the differ-
ence of time obtained from the update method in each frame.
CGPoint bgVelocity = CGPointMake(-SPACE_BG_VELOCITY, 0);
CGPoint amtToMove =
CGPointMultiplyScalar(bgVelocity,self.diffTime);
After that, the calculated distance is added in the current position of SpaceBGNode.
spaceBGNode.position = CGPointAdd(spaceBGNode.position,
amtToMove);
The last but the most important step to enable scrolling, is to set the position of
SpaceBGNode to the right edge of the screen whenever it reaches the left edge of the
screen.
Search WWH ::




Custom Search