扫雷06 触摸事件
创建触摸事件
在cocos2.2.3的时候,设置触摸比较麻烦,并不是所有控件都可以直接设置触摸事件。这里本来之前的方法是可以的用的,为了转移到3.x上来,就不用了,改用3.x的触摸对象。
1: //创建一个单点触摸实例
2: @H_403_74@auto evt = EventListenerTouchOneByOne::create();
3: evt->setEnabled(true);
4: evt->onTouchBegan = [&](Touch * pTouch,Event * pEvent){
5: @H_403_74@return @H_403_74@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: @H_403_74@return @H_403_74@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,@H_403_74@this);
实现触摸回调函数
1: /////////////////////////////////////////////////////////////////////////
2: // ccTouch
3: @H_403_74@bool GameScene::callTouchBegan(Touch * pTouch,Event * pEvent)
4: {
5: @H_403_74@return _isContinue; //如果不再继续游戏,就不再往下传递了
6: }
7:
8: // Touch 判断点击的位置以及更新图片
9: @H_403_74@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: @H_403_74@if (!mb->boundingBox().intersectsRect(CCRectMake(pt.x,pt.y,1,1))) {
20: @H_403_74@continue; // 下一个
21: }
22: // 点击的位置在这个精灵的范围之内
23: @H_403_74@if (mb->getDig()) {
24: @H_403_74@return; //已经被挖开
25: }
26: @H_403_74@char filename[16] = { 0 };
27: @H_403_74@int t = mb->getValue();
28: // 如果是去标记
29: @H_403_74@if (_toFlag) {
30: @H_403_74@const @H_403_74@char *ptx = mb->getFlag() ? "no.png" : "flag.png";
31: mb->setTexture(CCTextureCache::sharedTextureCache()->textureForKey(ptx));
32: mb->setFlag(!mb->getFlag());
33: @H_403_74@return;
34: }
35: //踩到雷------------------------------------------------------------------------
36: @H_403_74@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_500_403@ 42: @H_403_74@if (mine->getValue() == -1) {
43: mine->setTexture(CCTextureCache::sharedTextureCache()->textureForKey("-1.png"));
44: }
45: }
46: //显示爆炸
47: mb->setTexture(CCTextureCache::sharedTextureCache()->textureForKey("boom.png"));
48: @H_403_74@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: @H_403_74@goto CONTINUEGAME; //到此结束
58:
59: } //end CCARRAYDATA_FOREACH...
60: CONTINUEGAME:
61: _isContinue=ContinueGame();
62: }