Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

EDIT FOR CODE SNIPPET

init

voidNode = CCParallaxNode::node();
parta = CCSprite::spriteWithFile("backg.png");      
parta->setScaleX( 5 );
parta->setAnchorPoint(ccp(0,0));    
parta->setPosition(ccp(0,0));
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));
voidNode->runAction(repeat);

update

// 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 );
parta->setAnchorPoint(ccp(0,0));
voidNode->addChild(parta, -1, ccp(0.4f,0.5f), ccp(maxscroll,0));
maxscroll += parta->getContentSize().width*5;
}
share|improve this question
    
A code snippet might help. –  Byte56 Mar 14 '12 at 16:21
    
Added, I have changed it a lot this past hours, specially the if, so it may not be most perfect version. –  Ef Es Mar 14 '12 at 17:02

2 Answers 2

You could use something that someone has already created.

http://ak.net84.net/iphone/creating-a-repeating-backgroundinfinite-parallax-effect-in-cocos2d/

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!)

share|improve this answer
    
Good! I'm working on porting it. –  Ef Es Mar 15 '12 at 14:13

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

http://www.raywenderlich.com/3611/how-to-make-a-space-shooter-iphone-game

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.