3
\$\begingroup\$

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.

\$\endgroup\$
0

4 Answers 4

1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

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