Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

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:

After that you can trigger the background swap with CCActions.

share|improve this answer

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

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

Your Answer


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.