Skip to main content
edited tags
Link
CodeSmile
  • 1.7k
  • 2
  • 15
  • 16
Source Link
CodeSmile
  • 1.7k
  • 2
  • 15
  • 16

How to create endless scrolling landscape using coco2d's CCParallaxNode

I created a horizontally scrolling landscape using CCParallaxNode. However it does not seem to support endless scrolling in one direction. As soon as the first image "ends" the background shines through. Adding an unlimited number of images is of course not an option. I thought about the usual way, namely adding two sprites for each parallax layer next to each other, and simply moving one at the end of the other at the appropriate time.

However, I have two issues:

  • parallax child nodes can't seem to be re-positioned
  • the position of the child nodes vs how far they have moved seem to be in no relation to the parallax factor

This is how I set up the parallax node using 4 sprites with identical horizontal width:

CGPoint topOffset = CGPointMake(0, screenSize.height);
CGPoint midOffset = CGPointMake(0, screenSize.height / 2);
CGPoint downOffset = CGPointZero;

// Create a parallax node and add the sprites to it.
CCParallaxNode* paraNode = [CCParallaxNode node];
[paraNode addChild:para1 z:1 parallaxRatio:CGPointMake(0.5f, 0)
positionOffset:topOffset];
[paraNode addChild:para2 z:2 parallaxRatio:CGPointMake(1, 0) positionOffset:topOffset];
[paraNode addChild:para3 z:4 parallaxRatio:CGPointMake(2, 0) positionOffset:midOffset];
[paraNode addChild:para4 z:3 parallaxRatio:CGPointMake(3, 0) positionOffset:downOffset];
[self addChild:paraNode z:0 tag:ParallaxSceneTagParallaxNode];

Did anyone succeed in using CCParallaxNode for seamlessly endless scrolling?