It is my understanding that we are now able to use frame-animated sprites, published from Cocos Studio as JSON in our Cocos2d-js v3.3 projects. Unfortunately, I cannot seem to find instructions for how to preload and then use these resources in the javascript code.
I’ve seen the example parkour game that uses frame animated widgets and skeletal animation, but neither of those is exactly what I’m looking for.
I am looking for a sample of code, or a link to an example or tutorial that will convey an understanding of how this is done.
gOzaru, it seems like that information is about cocos2d-x, rather than cocos2d-js. El_Jorge, even though the title of that thread refers to js, it looks like the answers are about x, as well.
nite, thank you for the sample code. If you change the second line to: this.addChild(obj.node) it runs without error. It does not however result in an animation appearing on the screen.
I figured it out, if anyone interested…
If you using Cocos Studio v2.2.8 do not create project in it, it will use Cocos2d-JS 3.5, create project using CLI Cocos2d-JS v3.6.1
Then, copy paste .json from res of Cocos Studio project into res of project you created using Cocos2d-JS v3.6.1
Code will work perfectly in Xcode for MAC and iOS.
For people having a black screen: Make sure that the plist and png used by your animation is within the searchpath of cocos2dx.
At least for me it was a big problem that I exportet the animation from Cocos Studio and copied it into a subfolder within my resourcefolder of cocos2dx. The animation wouldn’t find the plist and png file since it didn’t know about the subfolder. Anyway, my code:
// adjust the search path
FileUtils::getInstance()->addSearchPath("Animation");
// create and place the node
std::string filename = “Animation/animation.csb”;
Node* node = CSLoader::createNode(filename);
node->setPosition(Point(
origin.x + visibleSize.width / 2,
origin.y + visibleSize.height / 2));
// create the animation and add it to the node
cocostudio::timeline::ActionTimeline* action = CSLoader::createTimeline(filename);
node->runAction(action);
// start the animation (and enable loop)
action->gotoFrameAndPlay(0, true);
@tufantalan I tried the code you posted, it loads the animation and plays it. My animation is a simple walk cycle, so I’d like the node to move its position around as well, but I can’t seem to change the position. I’ll use the variable names you posted to make things easier.
Is your setPosition function trigerred in a loop or etc? If you do not trigger many times setPosition it does notihing. It works only once.
Edit: Actually you can use moveBy or moveTo methods. They serve your purpose.
ccs.csLoader.createNode() function returns a node note sprite. Maybe cause of that. I touchMove event and I am changing x property of node like the following;
onTouchMoved: function (touch, event) {
cc.log("tasinimyor");
var target = event.getCurrentTarget();
var delta = touch.getDelta();
target.x += delta.x;
target.y += delta.y;
}
Update: So as I said before, the animation does play, but the node stays still. However, I noticed when I resized my Chrome window, the node was in a different place. Noticing this, I “wiggled” the screen size and my node was moving like it was supposed to.
So it appears to be some issue with cocos not updating the node values or not repainting the screen correctly. Does that make sense? What can I do about it?