cocos2d-x遊戲開發 跑酷(三) 人物跑動

來源:互聯網
上載者:User

原創,轉載請註明出處: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;}


好了,跑起來了


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.