接上一篇文章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; }
这篇写的比较简单 不作多解释了