nosmirck
nosmirck

Reputation: 666

Switch sprite animations in cocos2d-x

I have a "GameCharacter" class where I have a sprite for my main character in the game, I loaded the frames and animations correctly. I can switch between animations with single touches or swiping gestures. Right now I can rotate between animations.

In my touch function of my scene I have this:

_mychar->RunAnimation(id);

where id is the ID of the animation, just an integer I change with every touch.

the method works like this:

void GameCharacter::RunAnimation(int a){

    _sprite->stopAllActions();
    if(a<_animation.size() && a>=0){
        _sprite->runAction( CCRepeatForever::create(_animation[a]));
    }
}

_animation[] is just a vector with CCAnimate* objects retained (I release them later when I destroy the GameCharacter).

I have 2 idle animations, I want to switch between them randomly, for example, I want to "queue" the first animation 3 to 5 times and the second 1 to 2 times, and repeat like this forever... also, I want to interrupt this "infinite random idle animation" with a touch that makes the character run another animation until it ends (for example a jump animation) and when it ends, just get to the idle animation. I don't know if I explained well, I hope you can understand me.

The id for the animations are:

Right now, i just need to be able to get the character in that idle state (random times between animation 0 and 1) and whenever touch, the jump animation is done once and at the end the idle start again...

I will solve the walking animation later.

Thanks in advance!

Upvotes: 0

Views: 1566

Answers (1)

ssantos
ssantos

Reputation: 16536

I'd recommend schedule method to periodically run your idle logic, something like this.-

this->schedule(schedule_selector(YourGameClass::idleLogic), FREQUENCY_IN_SECONDS);

In your idleLogic method, you may check if your character isn't running nor jumping, and if so, get a random index for your idle animations array, and run _animation[index]. It'd be also a good idea to keep track of how much time your character has been stopped, so that you don't run any idle animation unless iddleTime >= MIN_IDLE_TIME

Hope it helps.

Upvotes: 1

Related Questions