cocos3.x 扫雷06 触摸事件
发布时间:2020-12-14 19:31:54 所属栏目:百科 来源:网络整理
导读:扫雷 06 触摸事件 创建触摸事件 在 cocos2.2.3 的时候,设置触摸比较麻烦,并不是所有控件都可以 直接 设置触摸事件。这里本来之前的方法是可以的用的,为了转移到 3.x 上来,就不用了,改用 3.x 的触摸对象。 1: //创建一个单点触摸实例 2: auto evt = Even
扫雷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;
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: }
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- 教你轻松在React Native中集成统计的功能
- flex3+struts 1.3+spring+ibatis 2.x整合代码实例
- windows中oracle 11g安装图解
- actionscript-3 – Flex AIR – IntelliJ中的app
- SQLite查询问题
- ruby-on-rails – 对Ruby on Rails多语言翻译管理
- c# – LoadLibrary,FreeLibrary和GetModuleHandl
- Flex、Spring整合:Spring BlazeDS Integration
- 无法使用Xcode 6 GM Seed提交给AppStore
- 16.7 Swift基于多层链接的可选链
热点阅读