This is the final part of series "Import Spine Animation in Cocos2d-x". I will use the materials which we made from previous post to import to Cocos2d-x and make it animate. I am going to use Visual Studio 2015 to code and build game. I created a project "YumikiTutorial" from Cocos console. To open the VS 2015 cocos2d-x solution in relative path "..\YumikiTutorial\proj.win32" and open "YumikiTutorial.sln". After opening this, VS will display the projects like this:
![]() |
Fig 1. Whole Cocos2d-x project in Visual Studio 2015. |
Open the AppDelegate.cpp, find and comment the code lines like screen shot below:
![]() |
Fig 2. Comment all unnecessary code lines. |
Add this code line to bottom of the code lines which you commented above:
glview-setFrameSize(800, 600);
![]() |
Fig 3. Set screen size of game to 800 x 600. |
Open "HelloWorldScene.cpp", insert these codes in the end of "HelloWorld::init()" method:
spine::SkeletonAnimation* player = spine::SkeletonAnimation::createWithFile("skeleton.json", "skeleton.atlas");
player->setScale(0.5f);
player->setSkin("default");
player->addAnimation(0, "animation", true);
player->setPosition(Vec2(visibleSize.width + origin.x, visibleSize.height / 2 + origin.y));
this->addChild(player, 2);
MoveTo* moveTo = MoveTo::create(20, Vec2(0 + origin.x, player->getPositionY()));
player->runAction(moveTo);
![]() |
Fig 4. Add those codes in the end of init method. |
That's it. You can see that in code, we will create a sprite object which is defined by the position of image and declared in json and atlas. Now you build and run the game, you can see that the worm will move from right to left with animation:
![]() |
Fig 5. The worm sprite is moving with animation. |
There are many interesting things need to explore in Spine and Cocos2d-x. And I hope this topic can help you to understand the basic and base on this to have some advance research and share your knowledge to everyone.
If you are interested in my posts, please like and follow to have my further posts' notification. Thank you!
Yumiki.
No comments :
Post a Comment