Game Development Reference
In-Depth Information
6.
In ERGBackground.m , write the method itself:
+ (ERGBackground *)generateNewParallax
{
ERGBackground *background = [[ERGBackground alloc]
initWithImageNamed:@"parallax.png"];
background.anchorPoint = CGPointMake(0, 0);
background.name = parallaxName;
background.position = CGPointMake(0, 0);
background.zPosition = 4;
return background;
}
7. Here, we create the new sprite node, set its position and anchor point for our
convenience, and adjust its position on the z axis. Within the same file, in the
generateNewBackground method, change the assigned zPosition value
from 1 to 5 so that it appears above the parallax layer.
8. And finally, in the update: method in ERGMyScene.m , add code to handle the
moving and creating of new parallax layers:
[self enumerateChildNodesWithName:parallaxName
usingBlock:^(SKNode *node, BOOL *stop) {
node.position = CGPointMake(node.position.x -
parallaxMoveSpeed * timeSinceLast, node.position.y);
if (node.position.x < - (node.frame.size.width + 100)) {
// if the node went completely off screen (with some
extra pixels)
// remove it
[node removeFromParent];
}}];
if (self.currentParallax.position.x < -500) {
// we create new background node and set it as current
node
ERGBackground *temp = [ERGBackground generateNewParallax];
temp.position = CGPointMake(self.currentParallax.
position.x + self.currentParallax.frame.size.width, 0);
[self addChild:temp];
self.currentParallax = temp;
}
 
Search WWH ::




Custom Search