cocos3.x 扫雷06 触摸事件

前端之家收集整理的这篇文章主要介绍了cocos3.x 扫雷06 触摸事件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

扫雷06 触摸事件

创建触摸事件

cocos2.2.3的时候,设置触摸比较麻烦,并不是所有控件都可以直接设置触摸事件。这里本来之前的方法是可以的用的,为了转移到3.x上来,就不用了,改用3.x的触摸对象。

 1: //创建一个单点触摸实例
 2: auto evt = EventListenerTouchOneByOne::create();
 3: evt->setEnabled(true);
 4: evt->onTouchBegan = [&](Touch * pTouch,Event * pEvent){
 5:     return this->callTouchBegan(pTouch,pEvent);
 6: };
 7: evt->onTouchEnded = [&](Touch * pTouch,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: #f4f4f4"> 8:     return this->callTouchEnded(pTouch,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: white"> 9: };
 10: _eventDispatcher->addEventListenerWithSceneGraPHPriority(evt,this);

实现触摸回调函数

函数实现点击到雷区方块后显示其所代表的图片

一下代码只是简单的实现踩到对应的方块后显示对应的图片

1: /////////////////////////////////////////////////////////////////////////
 2: // ccTouch
 3: bool GameScene::callTouchBegan(Touch * pTouch,Event * pEvent)
 4: {
 5:     return _isContinue;    //如果不再继续游戏,就不再往下传递了
 6: }
 7:
 8: // Touch 判断点击的位置以及更新图片
 9: void GameScene::callTouchEnded(Touch * pTouch,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: #f4f4f4"> 10: {
 11:     Point pt = pTouch->getLocation();
 12:
 13:     CCObject * obj;
 14:
 15:     CCARRAYDATA_FOREACH(_ArraySprite->data,obj)
 16:     {
 17:         MineBlock * mb = (MineBlock*)obj;
 18:         //如果点击的这个像素不在这个精灵所在的矩形范围内
 19:         if (!mb->boundingBox().intersectsRect(CCRectMake(pt.x,pt.y,1,1))) {
 20:             continue;    // 下一个
 21:         }
 22:         // 点击的位置在这个精灵的范围之内
 23:         if (mb->getDig()) {
 24:             return;        //已经被挖开
 25:         }
 26:         char filename[16] = { 0 };
 27:         int t = mb->getValue();
 28:         // 如果是去标记
 29:         if (_toFlag) {
 30:             const char *ptx = mb->getFlag() ? "no.png" : "flag.png";
 31:             mb->setTexture(CCTextureCache::sharedTextureCache()->textureForKey(ptx));
 32:             mb->setFlag(!mb->getFlag());
 33:             return;
 34:         }
 35:         //踩到雷------------------------------------------------------------------------
 36:         if (t == -1) {
 37:             _count = -1;    //设置计数为-1,代表输了
 38:             //显示所有雷
 39:             CCARRAYDATA_FOREACH(_ArraySprite->data,monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 16pt; padding-right: 0px; background-color: #f4f4f4"> 40:             {
 41:                 MineBlock * mine = (MineBlock*)obj;
@H_89_403@ 42:                 if (mine->getValue() == -1) {
 43:                     mine->setTexture(CCTextureCache::sharedTextureCache()->textureForKey("-1.png"));
 44:                 }
 45:             }
 46:             //显示爆炸
 47:             mb->setTexture(CCTextureCache::sharedTextureCache()->textureForKey("boom.png"));
 48:             goto CONTINUEGAME;    //到此结束
 49:         }    // end if t == -1
 50:
 51:         //没有踩到雷,但是周围有雷
 52:             //在移植到安卓的时候,全部精灵的_value都为0,出现错误,sprintf函数没有问题
 53:             sprintf(filename,"%d.png",t);
 54:             mb->setTexture(CCTextureCache::sharedTextureCache()->textureForKey(filename));
 55:             mb->setDig(true);
 56:             ++_count;    //扫区计数加一
 57:             goto CONTINUEGAME;    //到此结束
 58:
 59:     }    //end CCARRAYDATA_FOREACH...
 60:     CONTINUEGAME:
 61:     _isContinue=ContinueGame();
 62: }
原文链接:https://www.f2er.com/cocos2dx/346017.html

猜你在找的Cocos2d-x相关文章