[Cocos Studio] run animation from .csb on different sprite [SOLVED]

Hello,
I’ve been working on a cocos2dx project for some time. I’ve been using spritesheets and plists to run animations. Recently I’ve been trying to figure out how to use Cocos Studio’s embedded animation designer, which looks pretty awesome :smile:

I’ve managed to figure out how to load a node from CSB and then run an animation attached it from the timeline, like in this code:

Node* someNode = CSLoader::createNode("TutorialAnim.csb");
auto sprite = static_cast<Sprite*>(someNode->getChildByName("Sprite_1"));
_rootNode->addChild(sprite);

cocostudio::timeline::ActionTimeline *timeLine = CSLoader::createTimeline("TutorialAnim.csb");
timeLine->retain(); //released later on
sprite->runAction(timeLine);
timeLine->play("animation", true);

As you can see, I load and run the animation on the sprite from the same .csb file and it works. However, I’m wondering if there is a way to run the animation on any generic object, similiarily how you can achieve i with Animate::create() and AnimationCache::getInstance()->getAnimation("")

Loading the animation from the timeline and running it on any other object either does not work, or crash with EXC_BAD_ACCESS, which is weird, as I retain the timeLine object, for example:

auto otherSprite = Sprite::createWithSpriteFrameName("someSpriteName.png"); //loads correctly, does not crash
_rootNode->addChild(otherSprite);

cocostudio::timeline::ActionTimeline *timeLine = CSLoader::createTimeline("TutorialAnim.csb");
timeLine->retain(); //released later on
otherSprite->runAction(timeLine);
timeLine->play("animation", true); //sometimes EXC_BAD_ACCESS, sometimes nothing

Is this expected that you can run ActionTimeline objects only on nodes related to the same CocosStudio file? Or is there some property that i should set on the ActionTimeline for it to work on other nodes? I tried setTarget(), setOriginalTarget(), resume() etc. to no success.

So, in short: is there a way to run an ActionTimeline animations on generic objects not loaded from .csb?

Thanks in advance!

From my understanding, the frames in the animation hold reference of the Node which they are supposed to animate in the form of Node name. So if you want to run the ActionTimeline animations on another object not loaded from the same CSB file, you will have to make sure that the object you are running the animation has the Nodes in it that correspond to the names of the Nodes referenced by the frames.

For example, if the animation is animating a Node with the name of “Node1”, then the Node which is running this animation timeline needs to have a Node with the name of “Node1”. It does not matter if they are from the same CSB file.

I know this from a mistake whereby I run an ActionTimeline on a node from different CSB. The timeline runs because I have the Node with the same names as the original CSB file.

1 Like

I played around with ActionTimelines and Timelines, as your post about the Node reference pushed me towards the right direction.

I made sure that both the node I created from code and its parents had the exact same name as the nodes originally referenced in the CSB file, then I started to get EXC_BAD_ACCESS from TextureFrame::onEnter() . I guess it was probably that the TextureFrame trying to access the cache for a spriteframe with some wrong spriteframe name. I didn’t really feel like digging down all the way through the sourcecode to find the reason for the bad name, so I different way to solve to the problem.

Every Timeline can have its target set through the setNode() method:

auto sprite = Sprite::createWithSpriteFrameName("sprite-frame-name.png");
ActionTimeline *actionTimeline = CSLoader::createTimeline("CocoStudioTimeLine.csb");
actionTimeline->retain();
_rootNode->addChild(sprite);

sprite->runAction(actionTimeline);
Timeline *currentTimeLine = actionTimeline->getTimelines().at(0); //access Timeline from Vector
currentTimeLine->setNode(sprite);
actionTimeline->play("animation_name", true);

This enabled me to properly run Timeline actions on different nodes. It didn’t need setting the names of nodes to correspond to the ones from the .csb file.

Thanks for your help, duracellrabbid!

1 Like

@Qbeorama, many thanks for posting this. I have been looking for this all around since couple of days. You saved my time buddy :smile: .

There are great tutorials on Sonarlearning site around using cocos studio but some advanced tutorials for stuff like this will be much appreciated (mostly using cocos studio widgets in code).

Great solution, works with JS too:

// Get timelines
var timeLines = actionTimeLine.getTimelines();
for (timeLine of timeLines ) {
timeLine.setNode(animatedNode);
}