原創,轉載請註明出處:http://blog.csdn.net/dawn_moon/article/details/21245881
好吧,終於要跑起來了。要實現跑酷需要用到幀動畫,什麼是幀動畫,不解釋行麼。
介紹一個將小圖打包的工具TexturePacker,這是一個很強大的工具,另外還有一個物理編輯器PhysicsEditor,也不錯,地址:http://www.codeandweb.com。剛好上周收到工具作者給的free licences,感謝一下。有興趣的同學可以去申請一下。
載入精靈的一個比較高效的方式是用CCSpriteFrameCache和CCSpriteBatchNode配合使用。先講一下這個用法。
以下幾個步驟:
1.將打包好的大圖和產生的plist檔案載入到CCSpriteFrameCache,如果這兩個檔案名稱除尾碼不同其它完全一樣的話,直接載入plist檔案就可以了。
2.用大圖產生一個CCSpriteBatchNode對象batch_node,並將其加入到當前情境addChild(batch_node)。
3.通過CCSprite::spriteWithSpriteFrame或CCSprite::spriteWithSpriteFrameName建立精靈。
4.將上一步產生的精靈通過addChild加入到bath_node即可。
後面會有詳細代碼
建立Runner.cpp
//// Runner.h// Parkour//// Created by lerry on 14-3-14.////#ifndef __Parkour__Runner__#define __Parkour__Runner__#include "cocos2d.h"#include "Box2D.h"#include "resources.h"enum RunnerState{ running, jumping, crouch};class Runner : public cocos2d::CCNode{ cocos2d::CCSprite* mRunner; cocos2d::CCSize mRunningSize; cocos2d::CCAction* mRunningAction; cocos2d::CCSpriteBatchNode* mBatchNode; b2World* mWorld; RunnerState mState; private: void initAction(); void initBody(); void initShape(); public: bool init(b2World* world, cocos2d::CCSpriteBatchNode* batchNode); virtual void update(float dt); static Runner* create(b2World* world, cocos2d::CCSpriteBatchNode* batchNode); };#endif /* defined(__Parkour__Runner__) */
繼承CCnode,因為後面會有一個物理世界,所以重寫了create函數和init函數,這兩個函數還是保持與引擎一致,建立的對象也是自動釋放對象。本節沒有加入物理世界的實現,只是實現一個跑動的人物。
//// Runner.cpp// Parkour//// Created by lerry on 14-3-14.////#include "Runner.h"USING_NS_CC;Runner* Runner::create(b2World *world, CCSpriteBatchNode* batchNode){ Runner* runner = new Runner(); if (runner && runner->init(world, batchNode)) { runner->autorelease(); return runner; }else { delete runner; runner = NULL; return NULL; }}bool Runner::init(b2World *world, CCSpriteBatchNode* batchNode){ if (!CCNode::init()) { return false; } // box2d的物理世界 this->mWorld = world; this->mBatchNode = batchNode; initAction(); initBody(); initShape(); mRunner = CCSprite::createWithSpriteFrameName(runner0); // 通過名字擷取animation CCAnimation* animation = CCAnimationCache::sharedAnimationCache()->animationByName("running"); mRunner->setPosition(ccp(85, 50)); mRunner->runAction(CCRepeatForever::create(CCAnimate::create(animation))); mBatchNode->addChild(mRunner); return true;}void Runner::update(float dt){ }void Runner::initAction(){ CCArray* animateFrames = CCArray::create(); char str[50] = {0}; for (int i = 0; i != 8; ++i) { sprintf(str, "runner%d.png", i); animateFrames->addObject(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(str)); } CCAnimation* animation = CCAnimation::createWithSpriteFrames(animateFrames, 0.1); animation->setRestoreOriginalFrame(true); // animation 命名 CCAnimationCache::sharedAnimationCache()->addAnimation(animation, "running"); }void Runner::initBody(){ }void Runner::initShape(){ }
mWorld和mBatchNode會由PlayScene傳進來。這裡主要實現了initAction。動畫的實現與JS版的稍稍有點區別。
由於命名問題,我將資源檔又重新命名了一下
//// resources.h// Parkour//// Created by lerry on 14-3-13.// Copyright (c) 2014年 Goonear Co.,Ltd. All rights reserved.//#ifndef Parkour_resources_h#define Parkour_resources_hstatic const char backmusic[] = "background.mp3";static const char jummpmusic[] = "jump.mp3";static const char crouchmusic[] = "crouch.mp3";static const char spritesheet[] = "parkour.plist";static const char spritePacker[] = "parkour.png";static const char runner0[] = "runner0.png";#endif
現在需要修改PlayerScene的init函數,加入人物,變成下面這樣:
bool PlayScene::init(){ if(!CCLayer::init()){ return false; } initPhysics(); CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(spritesheet); CCSpriteBatchNode* spriteBatch = CCSpriteBatchNode::create(spritePacker); this->addChild(spriteBatch); mRunner = Runner::create(mWorld, spriteBatch); this->addChild(mRunner); // 調用update函數 scheduleUpdate(); return true;}
好了,跑起來了