接上一篇文章ios
本章咱們將實現兩個事情 數組
第一個、在地鼠鑽出來以後 執行一個笑的動畫dom
第二個、在地鼠鑽出來的時候 咱們能夠打他 而後打到以後他會消失動畫
咱們新增一個方法createAnimate 建立一個笑臉動做this
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.15f); //動畫執行完成以後是否回到第一幀 animation->setRestoreOriginalFrame(true); //由這個動畫信息建立一個序列幀動畫。 return Animate::create(animation); }
建立動畫寫完了 接下來咱們修改下地鼠的執行動畫 將以前的延遲動畫改成如今的笑臉動畫,即地鼠出現以後執行一個笑臉,執行完成以後再鑽入地面spa
void PlayTheMouse::popMole(Sprite *mole){ //在此方法中 執行了一串動做,從地鼠鑽出地面 而後再鑽入地面 //第一個動做 地鼠上移 auto moveup = MoveBy::create(0.2f, Point(0, mole->getContentSize().height * moleScale)); //第三個動做 鑽入地面 auto movedown = moveup->reverse(); //接下來執行動做 mole->runAction(Sequence::create(moveup,this->createAnimate(),movedown, nullptr)); }
運行下代碼 咱們能夠看到 以前延遲的那0.5s 已經變成了地鼠的笑臉 code
接下來 咱們須要實現怎麼敲打地鼠 首先 咱們須要添加觸摸監聽事件事件
//添加觸摸監聽事件 auto eventTouchListener = EventListenerTouchOneByOne::create(); eventTouchListener->onTouchBegan = CC_CALLBACK_2(PlayTheMouse::onTouchBegan, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(eventTouchListener, this);
監聽事件添加完畢 接下來能夠寫敲打判斷了get
bool PlayTheMouse::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *unused_event){ //獲取點擊以後的座標值 Point touchPoint = touch->getLocationInView(); for(auto mole : moles){ if(mole->getBoundingBox().containsPoint(touchPoint) && mole->isVisible() && mole->getNumberOfRunningActions() != 0){ CCLOG("打中了 %f", mole->getPositionX()); mole->setVisible(false); } } return true; }
看代碼中 首先遍歷地鼠 animation
而後判斷三個條件
一、觸摸點是否和地鼠碰撞
二、地鼠是否顯示
三、地鼠是否有動做在執行
當三個條件都知足以後 才能夠肯定地鼠被打中
打中以後將地鼠設置爲不顯示狀態
// // 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); //建立地鼠大笑動畫 Animate* createAnimate(); public: virtual bool onTouchBegan(Touch *touch, Event *unused_event); }; #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); //添加觸摸監聽事件 auto eventTouchListener = EventListenerTouchOneByOne::create(); eventTouchListener->onTouchBegan = CC_CALLBACK_2(PlayTheMouse::onTouchBegan, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(eventTouchListener, this); 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)); //第三個動做 鑽入地面 auto movedown = moveup->reverse(); //接下來執行動做 mole->runAction(Sequence::create(moveup,this->createAnimate(),movedown, nullptr)); mole->setVisible(true); } 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.15f); animation->setRestoreOriginalFrame(true); return Animate::create(animation); } bool PlayTheMouse::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *unused_event){ //獲取點擊以後的座標值 Point touchPoint = touch->getLocationInView(); for(auto mole : moles){ if(mole->getBoundingBox().containsPoint(touchPoint) && mole->isVisible() && mole->getNumberOfRunningActions() != 0){ CCLOG("打中了 %f", mole->getPositionX()); mole->setVisible(false); } } return true; }
這篇寫的比較簡單 不作多解釋了