Hi!
I’m creating an animation and it’s showing correctly. My problem arises when I want to change the speed of an existing animation. I read on another post that we can use the Speed action for such purpose, but before trying that, I want to understand why modifying the Animation object is not working. So this is what I do:
GameScreen.cpp
//fill up spriteFrames
auto walkAnimation = Animation::createWithSpriteFrames(spriteFrames, 0.1f);
auto animate = Animate::create(walkAnimation);
//I create an enemy and pass the animate object in the constructor
mEnemies.push_back(std::make_unique<Enemy>(..., animate, ...));
Enemy.cpp
Enemy::Enemy(..., Animate* walkAnimation, ...) : ..., mWalkAnimation(walkAnimation), ...
{
mWalkAnimation -> retain();
//should I also retain the Animation object? I'm guessing so...
mWalkAnimation -> getAnimation() -> retain();
mEnemySprite -> runAction(RepeatForever::create(walkAnimation));
}
ExternalFile.cpp
void ChangeAction(Enemy* e)
{
e -> EnemySprite().stopAllActions;
//... lots of stuff, and conditionally, we want to run again the animation slower
e -> WalkAnimation -> getAnimation() -> setDelayPerUnit(0.5f); //slower than originally
e -> EnemySprite().runAction(RepeatForever::create(e -> WalkAnimation));
}
The code above produces the same animation, with the same speed, no matter which argument I pass to setDelayPerUnit. In the debugger, I can see perfectly that the Animation object inside the Animate object is actually changed, and therefore I don’t understand why it’s not working. I also noted that if I create (or clone) a new Animate object, it works, but I would like to understand why. My guess is that stopAllActions is removing some data required by Animate, but since I’m retaining both the Animate and the Animation object, I don’t understand how that is possible.
Thanks in advance.