Animate action just plays first frame and then nothing

Animate action just plays first frame and then nothing
0

I have this strange situation, hope you can help me here because I have 2 days trying to find the solution and I can’t find it.

I made 2 animate actions for applying to a Sprite child that also is a Sprite or a subclass of Sprite, I initialize them in my init()

Vector<SpriteFrame *> animFrames;
int acc = 0;

for (int i = 0; i < 3; i++, acc += _SMOKE_LOW_ANIM_W){
    
    auto frame = SpriteFrame::create(
        _PLAYER_SMOKE_LOW_ANIM_RESOURCE_PATH,
        Rect(acc, 0, _SMOKE_LOW_ANIM_W, _SMOKE_LOW_ANIM_H));
    
    
    animFrames.pushBack(frame);
}


smokeLow = Animate::create(Animation::createWithSpriteFrames(animFrames, 0.2f, CC_REPEAT_FOREVER));
smokeLow->setTag(SMOKEANIM_LOW_TAG);
smokeLow->retain();

animFrames.clear();
acc = 0;

for (int i = 0; i < 3; i++, acc += _SMOKE_HEAVY_ANIM_W){
    
    auto frame = SpriteFrame::create(
        _PLAYER_SMOKE_HEAVY_ANIM_RESOURCE_PATH,
        Rect(acc, 0, _SMOKE_HEAVY_ANIM_W, _SMOKE_HEAVY_ANIM_H)
                                    );
    
    animFrames.pushBack(frame);
}

smokeHeavy = Animate::create(Animation::createWithSpriteFrames(animFrames, 0.2f, CC_REPEAT_FOREVER));
smokeHeavy->setTag(SMOKEANIM_HEAVY_TAG);
smokeHeavy->retain();

but when I try to use them at the moment needed…

First issue, before run the actions, I have to stop them from the actions list, otherwise I get an assertion error
Assertion failed: (! ccArrayContainsObject(element->actions, action)), function addAction, file /Users/abrajam/Cocos2DX-Projects/SkyHunterTutorial/cocos2d/cocos/2d/CCActionManager.cpp, line 193., (this is not a big deal trouble, I can do the stopActionByTag() and continue with other things, but I really like to know why it happens because in my game I have other Sprites with animate actions created and executed the same way and they reproduce rightly, I’ve tried to get the target of the Animations before runned and I get a nullptr obviously, so please, help me track this issue).

The second issue is about made them run by the first way, and just shows the first frame of the animation, I tough that was the .png file so I changed to other already working just as a trial, but same error, tried also mading the action a RepeatForever and instead using the macro CC_REPEAT_FOREVER on the number of loops the animation is going to reproduce, but same result, I think they do the same.

If I run one Animate from the init it runs well until I try to change to the other one, also if I run another different action on the parent the animate on the child gets reproduced by the amount of time the parent action occurs.

I don’t know how but this is the way I got it working, instead of using:

stopActionByTag(SMOKEANIM_LOW_TAG);
this->runAction(smokeLow);

I’ve used:

if (getActionByTag(SMOKEANIM_LOW_TAG) != smokeLow)
    this->runAction(smokeLow);

I don’t know how but it worked, I really would like to know why.

@abrajam take a look at this:

int acc = 0;

    for (int i = 0; i < 3; i++, acc += _SMOKE_LOW_ANIM_W){
        
        auto frame = SpriteFrame::create(
            _PLAYER_SMOKE_LOW_ANIM_RESOURCE_PATH,
            Rect(acc, 0, _SMOKE_LOW_ANIM_W, _SMOKE_LOW_ANIM_H));
        
        
        animFrames.pushBack(frame);
    }


    smokeLow = Animate::create(Animation::createWithSpriteFrames(animFrames, 0.2f, CC_REPEAT_FOREVER));
    smokeLow->setTag(SMOKEANIM_LOW_TAG);
    smokeLow->retain();

    animFrames.clear();
    acc = 0;
  1. int acc = 0;
  2. you fill: animFrames.pushBack(frame);
  3. a few lines down you clear it: animFrames.clear();
  4. then you reset: acc = 0;
  5. then you repeat filling the animFrames vector with different frames.

perhaps there is a scope issue or something going on?

Thanks but no, I first tough that that was the problem, and tried redefining the Vector and variable acc inside the scope.

I already got it working, but I am not sure why it works one way and no another, I think is a thing related to Action class intern working and I really appreciate if someone could explain me.

So nobody says anything :thinking:
Anyway I found something on stackoverflow that also is related, but refers to cocos2d in objective-c, I haven’t tested since I haven’t had another action not running since the one above, so I don’t know if it still works or is an old bug that in earlier versions were solved, but it says that when an action not runs overloading onEnter() will gonna’ make it work

hope that somebody could say more about it and actions in general.

I’m not the expert, but i ll say what i know

The assertion error you mentioned is: “action already be added!”. If you need to use the same action more than once, use clone().

sprite->runAction(someAction->clone());

I can not answer exactly why you need to do this, but here they explain something:

Can you give me something to debug? (i don’t want the entire project ofc :laughing: but something i can use to trace the problem)

The thing is man that the action was not already added just retained to not be released before used but when I wanted to use it I couldn’t, until I found a solution but the solution have no sense at all, please read entirely.

I read all, but im trying to understand. I 'd retained actions too in my projects to use them later with no problems. But if i used more than once i use clone().

is the assertion being triggered when you call retain()? or when you try to runAction()?

i think is when you try to runaction, but are you sure is the first time runAction is executed with that action?

this if:

if (getActionByTag(SMOKEANIM_LOW_TAG) != smokeLow)
    this->runAction(smokeLow);

just tells me that you are checking if the action is running on the target. So yes it wont run twice if is already running. So that solves the problem of adding it twice or more.

Shit, yes, I just checked my update and of course every frame this is trying to run the action and of course if it checks before running it just runs once and also if I stop it before running it ,it never will going to be completed so that is the reason why wasn’t apparently showing, my bad :frowning_face:.

1 Like

Happy you found it! :smiley:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.