此教程來自於子山龍人的cocos2d版本 http://www.cnblogs.com/zilongshanren/archive/2011/05/15/2045784.htmlhtml
子山龍人的教程是屬於cocos2d版本的 如今邊學學作成cocos2d-x3.0版本ios
本例子的git地址:http://git.oschina.net/gejw0623/cocos2d-x3.0_Sample.gitgit
本教程的資源文件:http://pan.baidu.com/s/1qW2PWXu數組
接下來 咱們建立一個類 用於本教程的開發 我在這建立了一個 PlayTheMouse 類 繼承了 Layerdom
咱們的背景層圖片爲 『bg_dirt.png』
this
將背景層添加代碼加到init()初始化方法中spa
//添加背景 auto background = Sprite::create("bg_dirt.png"); //因爲子山的博客裏面用的素材不是針對800x480的 因此咱們要計算一個縮放比 float scale = background->getContentSize().width * 2 / winSize.width; //同理 因爲有一個縮放比 因此咱們在接下來使用地鼠高寬時 須要計算縮放以後纔會獲取正確值 moleScale = winSize.width / (background->getContentSize().width * 2.0f); background->setScale(scale); background->setPosition(Point(winSize.width / 2, winSize.height / 2)); //-2是z軸 在2d遊戲中 z軸用於顯示層級 z越大 就越在上層 this->addChild(background, -2);
咱們運行下代碼 看下效果.net
咱們將草坪分爲兩塊 一塊上部分 一塊下部分
code
代碼添加在背景層代碼下方htm
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("playthemouse.plist"); //下半部分的草坪 auto lower = Sprite::createWithSpriteFrameName("grass_lower.png"); lower->setAnchorPoint(Point(0.5, 1)); lower->setPosition(Point(winSize.width / 2, winSize.height / 2 + 1)); this->addChild(lower, 1); //上半部分的草坪 auto upper = Sprite::createWithSpriteFrameName("grass_upper.png"); upper->setAnchorPoint(Point(0.5, 0)); upper->setPosition(Point(winSize.width / 2, winSize.height / 2)); this->addChild(upper, -1);
爲了模擬出地鼠鑽出鑽入洞的效果 咱們將上部分z值設爲1 下部分設爲-1 地鼠設爲0
運行代碼 咱們能夠看到草坪出現了
咱們先在頭文件中添加一個Vector對象 用於保存三隻地鼠對象 方便接下來的代碼中進行循環處理
Vector<Sprite*> moles;
接下來 咱們在init方法中使用一個for循環來添加三隻小地鼠
//接下來添加三個小地鼠 而且初始化座標 for (int i = 0 ; i < 3; i++) { auto mole = Sprite::createWithSpriteFrameName("mole_1.png"); mole->setScale(moleScale); mole->setPosition(Point(155 + 245 * i, winSize.height / 2 - mole->getContentSize().height / 2 * moleScale - 30)); this->addChild(mole, 0); moles.pushBack(mole); }
運行代碼 咱們看下效果
~~~~~~哎呀 地鼠沒出現 等等 咱們將地鼠的z值改成2 在草坪上方
this->addChild(mole, 1);
接下來 咱們再運行下代碼 看看 地鼠出現了
可是 若是地鼠在洞下面的時候 咱們不應讓他顯示的 因此 咱們將z值改回來 改爲0
接下來咱們要讓地鼠動起來了
咱們須要執行一個方法 讓他每0.5s進行地鼠檢測 而且有1/3的機率讓地鼠鑽出地面
首先 咱們建立一個方法 此方法每0.5s執行一次
//此方法每0.5s執行一次 用於判斷每個地鼠 讓它有機會鑽出洞來 void updateMole(float dt);
接下來 咱們在init中使用schedule 控制0.5s執行一次
//每隔0.5s執行一次updateMole this->schedule(schedule_selector(PlayTheMouse::updateMole), 0.5f);
如今 咱們能夠在updateMole中寫關於檢測地鼠的代碼
void PlayTheMouse::updateMole(float dt){ //循環遍歷地鼠 for(auto mole : moles){ //計算1/3的機率可讓地鼠鑽出 if(arc4random() % 3 == 0){ //當地鼠沒有動做執行時 咱們讓他執行動做 if(mole->getNumberOfRunningActions() == 0){ this->popMole(mole); } } } }
代碼中註釋寫的很清楚 我就不解釋了
上面的代碼中有一個popMole方法 這個方法可讓地鼠執行一連串動做
void PlayTheMouse::popMole(Sprite *mole){ //在此方法中 執行了一串動做,從地鼠鑽出地面 而後再鑽入地面 //第一個動做 地鼠上移 auto moveup = MoveBy::create(0.2f, Point(0, mole->getContentSize().height * moleScale)); //第二個動做 用於延遲0.5s auto delay = DelayTime::create(0.5f); //第三個動做 鑽入地面 (reverse:返回一個反向動做,即從地面鑽入洞中) auto movedown = moveup->reverse(); //接下來執行動做 (Sequence:按序列執行動做,這會讓節點連續執行幾個動做) mole->runAction(Sequence::create(moveup,delay,movedown, nullptr)); }
好了 咱們運行代碼 能夠看到 三個地鼠在不停的鑽出 鑽入
不過 咱們如今也不能打它們 咱們怎麼打他們 下一章講
// // PlayTheMouse.h // study // // Created by Robin on 14-5-3. // // #ifndef __study__PlayTheMouse__ #define __study__PlayTheMouse__ #include <iostream> #include "cocos2d.h" USING_NS_CC; class PlayTheMouse : public Layer{ public: static Scene* createScene(); virtual bool init(); CREATE_FUNC(PlayTheMouse); private: Size winSize; //因爲咱們的素材不是按照800x480來的 因此須要計算一個縮放值 float moleScale; //用於保存地鼠的數組 Vector<Sprite*> moles; //此方法每0.5s執行一次 用於判斷每個地鼠 讓它有機會鑽出洞來 void updateMole(float dt); //彈出地鼠 void popMole(Sprite* mole); }; #endif /* defined(__study__PlayTheMouse__) */
// // PlayTheMouse.cpp // study // // Created by Robin on 14-5-3. // // #include "PlayTheMouse.h" Scene* PlayTheMouse::createScene(){ auto scene = Scene::create(); auto layer = PlayTheMouse::create(); scene->addChild(layer); return scene; } bool PlayTheMouse::init(){ bool bRet = false; do { winSize = Director::getInstance()->getWinSize(); SpriteFrameCache::getInstance()->addSpriteFramesWithFile("playthemouse.plist"); //添加背景 auto background = Sprite::create("bg_dirt.png"); float scale = background->getContentSize().width * 2 / winSize.width; moleScale = winSize.width / (background->getContentSize().width * 2.0f); background->setScale(scale); background->setPosition(Point(winSize.width / 2, winSize.height / 2)); this->addChild(background, -2); //下半部分的草坪 auto lower = Sprite::createWithSpriteFrameName("grass_lower.png"); lower->setAnchorPoint(Point(0.5, 1)); lower->setPosition(Point(winSize.width / 2, winSize.height / 2 + 1)); this->addChild(lower, 1); //上半部分的草坪 auto upper = Sprite::createWithSpriteFrameName("grass_upper.png"); upper->setAnchorPoint(Point(0.5, 0)); upper->setPosition(Point(winSize.width / 2, winSize.height / 2)); this->addChild(upper, -1); //接下來添加三個小地鼠 而且初始化座標 for (int i = 0 ; i < 3; i++) { auto mole = Sprite::createWithSpriteFrameName("mole_1.png"); mole->setScale(moleScale); mole->setPosition(Point(155 + 245 * i, winSize.height / 2 - mole->getContentSize().height / 2 * moleScale - 30)); this->addChild(mole, 0); moles.pushBack(mole); } //每隔0.5s執行一次updateMole this->schedule(schedule_selector(PlayTheMouse::updateMole), 0.5f); bRet = true; } while (0); return bRet; } void PlayTheMouse::updateMole(float dt){ //循環遍歷地鼠 for(auto mole : moles){ //計算1/3的機率可讓地鼠鑽出 if(arc4random() % 3 == 0){ //當地鼠沒有動做執行時 咱們讓他執行動做 if(mole->getNumberOfRunningActions() == 0){ this->popMole(mole); } } } } void PlayTheMouse::popMole(Sprite *mole){ //在此方法中 執行了一串動做,從地鼠鑽出地面 而後再鑽入地面 //第一個動做 地鼠上移 auto moveup = MoveBy::create(0.2f, Point(0, mole->getContentSize().height * moleScale)); //第二個動做 用於延遲0.5s auto delay = DelayTime::create(0.5f); //第三個動做 鑽入地面 (reverse:返回一個反向動做,即從地面鑽入洞中) auto movedown = moveup->reverse(); //接下來執行動做 (Sequence:按序列執行動做,這會讓節點連續執行幾個動做) mole->runAction(Sequence::create(moveup,delay,movedown, nullptr)); } Animate* PlayTheMouse::createAnimate(){ auto animation = Animation::create(); animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("mole_laugh1.png")); animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("mole_laugh2.png")); animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("mole_laugh3.png")); animation->setDelayPerUnit(0.1f); animation->setRestoreOriginalFrame(true); return Animate::create(animation); }
素材已經在開始給出