{"id":404,"date":"2012-07-28T14:42:24","date_gmt":"2012-07-28T14:42:24","guid":{"rendered":"http:\/\/www.blogs.abeazam.com\/dev\/?p=404"},"modified":"2012-11-20T16:33:51","modified_gmt":"2012-11-20T16:33:51","slug":"creating-frame-animation-in-cocos2d-x-2-0-using-a-sprite-sheet","status":"publish","type":"post","link":"http:\/\/www.blogs.abeazam.com\/dev\/2012\/07\/creating-frame-animation-in-cocos2d-x-2-0-using-a-sprite-sheet\/","title":{"rendered":"Creating Frame Animation in Cocos2d-X 2.0 Using a Sprite Sheet"},"content":{"rendered":"<p>Since the Cocos2d-X 2.0 rc2 a lot of the initialisation has changed so this code works in Cocos2d-X 2.0.<\/p>\n<p>First <a href=\"http:\/\/www.blogs.abeazam.com\/dev\/2012\/07\/create-cocos2d-x-2-0-sprite-sheet\/\">create a sprite sheet<\/a> so we can load it in.<\/p>\n<p>Once you have created the Sprite Sheet you will end up with a image file and plist.<\/p>\n<p>mySprites.png<br \/>\nmySprites.plist<\/p>\n<p>I like to name my sprites like this:<\/p>\n<p>jumpAnimation_00<br \/>\njumpAnimation_01<br \/>\njumpAnimation_02<br \/>\njumpAnimation_03<br \/>\njumpAnimation_04<br \/>\njumpAnimation_05<\/p>\n<p>walkAnimations_00<br \/>\nwalkAnimations_01<br \/>\nwalkAnimations_02<br \/>\nwalkAnimations_03<br \/>\nwalkAnimations_04<br \/>\nwalkAnimations_05<\/p>\n<p>It makes creating the animations easier. My code below is depended on the above naming convention.<\/p>\n<p>[cpp]<\/p>\n<p>void creatAnimation()<br \/>\n{<br \/>\n\/\/load plist and it will load in the png with the same name<br \/>\n\/\/the frames will be stored in the CCSpriteFrameCache so you can get the frames at you leisure<br \/>\nCCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(&#8220;mySprites.plist&#8221;);<\/p>\n<p>\/\/the frames will be gathered in the animation object<br \/>\nCCAnimation* jumpAnim = animationName(&#8220;jumpAnimations&#8221;);<br \/>\njumpAnim->setDelayPerUnit(0.2);<\/p>\n<p>\/\/create sprite first frame from animation first frame<br \/>\nCCSprite *jumpSprite = CCSprite::create((CCSpriteFrame*) jumpAnim->getFrames()->objectAtIndex(0));<\/p>\n<p>CCAction *jumpAct = CCRepeatForever::create(jumpAnim);<br \/>\njumpSprite->runAction(jumpAct);<br \/>\nthis->addChild(spriteAnim);<\/p>\n<p>}<br \/>\n\/\/this method loops through the CCSpriteFrameCache to get the<br \/>\nCCAnimation *animationName(char const *name)<br \/>\n{<br \/>\n\/\/creates a temp animation object<br \/>\nCCAnimation* holderAnim = CCAnimation::create();<\/p>\n<p>\/\/variables used for the loop<br \/>\nstring fName;<br \/>\nstring tName = name;<br \/>\nCCSpriteFrame* pFrame;<br \/>\nint frameCount = 1;<\/p>\n<p>\/\/loops till it can&#8217;t find any more frames<br \/>\ndo<br \/>\n{<\/p>\n<p>\/\/adds the underscore and a zero if the image is less than 10<br \/>\nfName = (frameCount < 10) ? tName + \"_0\" : tName + \"_\";\n\n\/\/adds the image number---Utils::num2str() converts an integer to a string\nfName = fName + Utils::num2str(frameCount);\n\nframeCount++; \/\/tries to get the frame\n\n\/\/tries to get the frame\npFrame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(fName.c_str());<\/p>\n<p>\/\/adds the frame to the animation holder<br \/>\nif (pFrame)<br \/>\nholderAnim->addSpriteFrame(pFrame);<\/p>\n<p>\/\/continues too loop till the next frame is null<br \/>\n} while(pFrame != NULL);<\/p>\n<p>return holderAnim;<br \/>\n}<\/p>\n<p>[\/cpp]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since the Cocos2d-X 2.0 rc2 a lot of the initialisation has changed so this code works in Cocos2d-X 2.0. First create a sprite sheet so we can load it in. Once you have created the Sprite Sheet you will end up with a image file and plist. mySprites.png mySprites.plist I like to name my sprites [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,82,83,84,18,25,26,30,40],"tags":[85],"_links":{"self":[{"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/posts\/404"}],"collection":[{"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/comments?post=404"}],"version-history":[{"count":10,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/posts\/404\/revisions"}],"predecessor-version":[{"id":431,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/posts\/404\/revisions\/431"}],"wp:attachment":[{"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/media?parent=404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/categories?post=404"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/tags?post=404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}