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 am working on a game development project for Android. We use the Cocos2d framework. I have to design a page which contains two background images, I need to switch these images repeatedly within 1sec. How can I do this? How can I use CCTransitionScene in my Java code and change these images repeatedly?

Thanks in advance.

share|improve this question
add comment

4 Answers 4

You can also make a CCRepeatForever of a CCSequence of the actions you want to perform, and make the scene run it.

share|improve this answer
add comment

You can't use CCTransitionScene to change just the background. What you are looking for is CCActions.

But first, here is a sample of a simple "animation".

First init a SpriteFrameCache (that will store all your sprites):

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"destructible-clock.plist"];


CCSprite *clock = [CCSprite spriteWithSpriteFrameName:@"destructible-clock3.png"];
clock.position =  _countdownTimer.position;

[self addChild:clock];

Then when you need to change the sprite use setDisplayFrame:

        //Update desctructible clock
        CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"destructible-clock%d.png",(int)(self.countdownTimer.percentage/25)]];
        [self.clock setDisplayFrame:frame];

It's important that you understand SpriteSheets and/or SpriteFrameCache. Try this if you can't figure out how to make this work: http://www.raywenderlich.com/1271/how-to-use-animations-and-sprite-sheets-in-cocos2d

After that you can trigger the background swap with CCActions.

share|improve this answer
add comment

I would suggest creating an animation and then use CCAnimate class to change frames repeatedly. creating CCAnimation Object is also an easy task, you have to initialize 2 frames using textures and then passing them to CCAnimate constructor.

share|improve this answer
add comment

A few approaches.

Create two background sprites and modify their Z-order to move one on top of the other.

Create two background sprites and change their visibility flags as needed.

Create one background sprite and swap its displayframe between the different background images.

share|improve this answer
add comment

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.