I have a Node named “Fruit” which contains 4 single frames for each fruit. It also contains a shadow, which should be the same for all fruits. I’m creating this node like this:
auto newFruit = CSLoader::createNode("Fruit.csb");
auto fruitAction = CSLoader::createTimeline("Fruit.csb");
newFruit->runAction(fruitAction);
Now when I’m creating this fruit I want to set a random frame:
fruitAction->gotoFrameAndPause(r + 1);
r is from 0 to 3.
However it doesn’t work. It doesn’t change frame at all. When I run debugging I can see correct frame number.
So I’ve tried different solution. I’ve made 4 1-frame animations named “a1”, “a2”, “a3” and “a4”.
Then:
fruitAction->play("a" + to_str(r + 1), false);
Now I’m getting sometimes good sometimes not. Giving constant r continuously is giving me different results.
Only solution I’ve found is to make all animation 2-frames long (with 1 offset) so “a1”: 0->1, “a2”: 2->3, "a3"4->5, “a4”:6->7, but this is too complicated to be worth using.
It seems that going to a frame and pausing it, does not actually renders the frame.
The gotoFrameAndPause function is normally used to set the start frame, pause the animation and play the animation from there afterwards if you resume it.
Ok, after hard debugging I figured out where’s the problem. Actually there are 2:
when you won’t play your animation it’ll have playing property set to false so ActionTimeline::step won’t even pass first if (checking if playing) and it’ll never render another frame.
when you use gotoFrameAndPause _endFrame is never set (it’s 0 by default) and because of that setCurrentFrame will always fail, because of this if:
if (frameIndex >= _startFrame && frameIndex <= _endFrame)
Well, I think it shouldn’t work like that.
So, first time you have to use gotoFrameAndPlay to properly setup _endFrame. Next you can use setCurrentFrame, but you have to resume, call step to force rendering new frame and pause of course.