I'm trying to create an infinite scroll using two images in a CCParallaxNode, making one move to the end of the scroll when it gets off the screen. I have tried several implementations but none has worked. The method setPosition is not available for the sprites once they have been added to the parallaxnode, and the array where the offset positions are stored is not documented so I don't know how to handle it.

Given that you cannot add the same child twice because the program crashes, I have tried to add a new sprite at the end whenever the old one dissappeared from screen. But the getPosition methods for either the sprites or the scroll doesn't seem to give accurate positions to calculate when a sprite is outside the screen. I ended up implementing a loop that added N sprites in a row and then "breaking" the program.

I was using a scaled image for the sprite with 512 width, scaled times 5. Screen size 1024x768



voidNode = CCParallaxNode::node();
parta = CCSprite::spriteWithFile("backg.png");      
parta->setScaleX( 5 );
voidNode->addChild(parta, -1, ccp(0.4f,0.5f), ccp(0,0));

maxscroll = parta->getContentSize().width*5;

CCMoveBy* moveRight = CCMoveBy::actionWithDuration(0.1f,ccp(-600, 0));  
CCRepeatForever* repeat = 
        CCRepeatForever::actionWithAction((CCSequence *)CCSequence::actions(moveRight,0));


// I tried with voidNode->getPos() and voidNode->m_tLastPosition.x too
int newPos = parta->getPosition().x;  
if (maxscroll - abs(newPos) - initPos) < level->getContentSize().width*5){

// 1st: parta->setPosition wont work, even when goten as child
// 2nd: I dont now how to access the m_pParallaxArray to alter the offset positions

// 3rd implementation
parta = CCSprite::spriteWithFile("backg.png");
parta->setScaleX( 5 );
voidNode->addChild(parta, -1, ccp(0.4f,0.5f), ccp(maxscroll,0));
maxscroll += parta->getContentSize().width*5;
  • \$\begingroup\$ A code snippet might help. \$\endgroup\$
    – House
    Commented Mar 14, 2012 at 16:21
  • \$\begingroup\$ Added, I have changed it a lot this past hours, specially the if, so it may not be most perfect version. \$\endgroup\$ Commented Mar 14, 2012 at 17:02

2 Answers 2


You could use something that someone has already created.


It looks like the developer who wrote that ran into some troubles using the out of the box object as well and as such he decided to write a custom solution and subsequently share it with the world!

Good programmers write good code. Great programmers steal great code. (Granted I haven't tested so maybe it's not that great. Who knows!)

  • \$\begingroup\$ Good! I'm working on porting it. \$\endgroup\$ Commented Mar 15, 2012 at 14:13
  • 2
    \$\begingroup\$ link is broken. \$\endgroup\$
    – Gigala
    Commented Sep 8, 2015 at 8:28

Check out this tutorial. It demonstrates what you are asking about.


Actually, I just noticed that you listed Cocos2D-X, which I am unfamiliar with. I don't know if the referenced tutorial will help, but I will leave the reference in case it does.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .