Cocos2d-x
发布时间:2022-05-03 发布网站:大佬教程 code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Cocos2d-x 3.0 触摸机制,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在Cocos2dx 3.0版本中,废弃了以往2.x版本的写法,我们先来看一下Layer.h中的一段代码:
1
2
3
4
5
6
7
8
9
10
11
|
virtual
bool
onTouchBegan(Touch*touch,Event*unused_event);
virtual
void
onTouchMoved(Touch*touch,Event*unused_event);
onTouchEnded(Touch*touch,Event*unused_event);
onTouchCancelled(Touch*touch,Event*unused_event);
onTouchesBegan(
const
std::vector<Touch*>&touches,Event*unused_event);
onTouchesMoved(
:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,Event*unused_event);
onTouchesEnded(
:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,Event*unused_event);
onTouchesCancelled(
:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,Event*unused_event);
|
单点触摸:(即只有注册的Layer才能接收触摸事件)
onTouchBegan
如果返回true:本层的后续Touch事件可以被触发,并阻挡向后层传递
如果返回false,本层的后续Touch事件不能被触发,并向后传递,也就是不会调用
onTouchMoved
简单点来说,如果:
1.Layer 只有一层的情况:
1
onTouchBegan(CCTouch*pTouch,CCEvent*pEvent);
|
a.返回false,则ccTouchMoved(),ccTouchEnded()不会再接收到消息
b.返回true,则ccTouchMoved(),ccTouchEnded()可以接收到消息
2.Layer 有多层的情况:
a.返回false,则本层的onTouchMoved(),onTouchEnded()不会再接收到消息,但是本层之下的其它层会接收到消息
b.返回true,则本层的onTouchMoved(),onTouchEnded()可以接收到消息,但是本层之下的其它层不能再接收到消息
单点触摸简单用法:
在Layer中添加如下代码,重写onTouchxxx函数
7
autodispatcher=Director::geTinstance()->getEventDispatcher();
autolistener=EventListenerTouchOneByOne::create();
listener->onTouchBegan=CC_CALLBACK_2(GameLayer::onTouchBegan,
this
);
listener->onTouchMoved=CC_CALLBACK_2(GameLayer::onTouchMoved,monospace!important; font-size:1em!important; min-height:inherit!important; color:black!important">);
listener->onTouchEnded=CC_CALLBACK_2(GameLayer::onTouchEnded,monospace!important; font-size:1em!important; min-height:inherit!important; color:black!important">);
listener->setSwallowTouches(
true
);
dispatcher->addEventListenerWithSceneGraphpriority(listener,monospace!important; font-size:1em!important; min-height:inherit!important; color:black!important">);
|
listener->setSwallowTouches(true),不向下触摸,简单点来说,比如有两个sprite,A 和 B,A在上B在下(位置重叠),触摸A的时候,B不会受到影响;
listener->setSwallowTouches(false)反之,向下传递触摸,触摸A也等于触摸了B;
多点触摸点单用法(多个Layer获取屏幕事件):
6
autolistener1=EventListenerTouchAllAtOnce::create();
listener1->onTouchesBegan=CC_CALLBACK_2(GameLayer::onTouchesBegan,monospace!important; font-size:1em!important; min-height:inherit!important; color:black!important">listener1->onTouchesMoved=CC_CALLBACK_2(GameLayer::onTouchesMoved,monospace!important; font-size:1em!important; min-height:inherit!important; color:black!important">listener1->onTouchesEnded=CC_CALLBACK_2(GameLayer::onTouchesEnded,monospace!important; font-size:1em!important; min-height:inherit!important; color:black!important">dispatcher->addEventListenerWithSceneGraphpriority(listener1,serif; font-size:14px"> 或者setTouchEnabled(true),然后重写layer的onTouchsxxx函数
关于eventDispatcher
1) 获取方法:
autodispatcher=Director::geTinstance()->getEventDispatcher();
事件监听器包含以下几种:
触摸事件 (EventListenerTouch)
键盘响应事件 (EventListenerKeyboard)
加速记录事件 (EventListenerAcceleration)
鼠标响应事件 (EventListenerMousE)
自定义事件 (EventListenerCustom)
以上事件监听器统一由 _eventDispatcher 来进行管理。
2)优先权:
1.优先级越低,越先响应事件
2.如果优先级相同,则上层的(z轴)先接收触摸事件
有两种方式将 事件监听器 listener1 添加到 事件调度器_eventDispatcher 中:
2
@H_618_342@EventDispatcher::addEventListenerWithSceneGraphpriority(EventListener*listener,Node*nodE)
EventDispatcher::addEventListenerWithFixedPriority(EventListener*listener,
int
fixedPriority)
代码展开一下:
11
12
13
14
:rgb(70,Node*nodE)
CCassERT(listener&&node,
"Invalidparameters."
);
CCassERT(!listener->isRegistered(),monospace!important; font-size:1em!important; min-height:inherit!important; color:blue!important">"THelistenerhasbeenregistered."
);
if
(!listener->checkAvailable())
return
;
listener->setSceneGraphpriority(nodE);
listener->setFixedPriority(0);
listener->setRegistered(
);
addEventListener(listener);
}
|
14
15
16
fixedPriority)
CCassERT(listener,monospace!important; font-size:1em!important; min-height:inherit!important; color:black!important">CCassERT(fixedPriority!=0,monospace!important; font-size:1em!important; min-height:inherit!important; color:blue!important">"0priorityisforbiddenforfixedprioritysinceit'susedforscenegraphbasedpriority."
);
(!listener->checkAvailable())
;
listener->setSceneGraphpriority(nullptr);
listener->setFixedPriority(fixedPriority);
listener->setPaused(
false
);
addEventListener(listener);
}
|
(1)addEventListenerWithSceneGraphpriority 的事件监听器优先级是0,而且在 addEventListenerWithFixedPriority 中的事件监听器的优先级不可以设置为 0,因为这个是保留给 SceneGraphpriority 使用的。
(2)另外,有一点非常重要,FixedPriority listener添加完之后需要手动remove,而SceneGraphpriority listener是跟node绑定的,在Node的析构函数中会被移除。
移除方法:
dispatcher->removeEventListener(listener);
其实还可以这么用:
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@H_683_607@
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//1加入用户触摸事件侦听
autolistener=EventListenerTouchOneByOne::create();
listener->onTouchBegan=[&](Touch*t,Event*E){
spHeadCol=spHead->getPositionX()/32;
spHeadRow=spHead->getPositionY()/32;
(
abs
(spHeadCol-col)>
(spHeadRow-row))
(spHeadCol<col)
spHead->m_dir=ENUM_DIR::DIR_RIGHT;
}
else
spHead->m_dir=ENUM_DIR::DIR_LEFT;
}
}
else
spHead->m_dir=ENUM_DIR::DIR_UP;
@H_19_801@
else
@H_675_809@
{
spHead->m_dir=ENUM_DIR::DIR_DOWN;
}
}
return
;
};
_eventDispatcher->addEventListenerWithSceneGraphpriority(listener,monospace!important; font-size:1em!important; min-height:inherit!important; color:black!important">);
|
大佬总结
以上是大佬教程为你收集整理的Cocos2d-x 3.0 触摸机制全部内容,希望文章能够帮你解决Cocos2d-x 3.0 触摸机制所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。 如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
|