微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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);
virtual void onTouchEnded(Touch*touch,Event*unused_event);
virtual void onTouchCancelled(Touch*touch,Event*unused_event);
//多点触摸
virtual void ontouchesBegan( const std::vector<Touch*>&touches,Event*unused_event);
virtual void ontouchesMoved( const std::vector<Touch*>&touches,Event*unused_event);
virtual void ontouchesEnded( const std::vector<Touch*>&touches,Event*unused_event);
virtual void ontouchesCancelled( const std::vector<Touch*>&touches,Event*unused_event);


单点触摸:(即只有注册的Layer才能接收触摸事件)

onTouchBegan

如果返回true:本层的后续Touch事件可以被触发,并阻挡向后层传递

如果返回false,本层的后续Touch事件不能被触发,并向后传递,也就是不会调用


onTouchMoved

简单点来说,如果:

1.Layer 只有一层的情况:

1
virtual bool onTouchBegan(CCTouch*pTouch,CCEvent*pEvent);

a.返回false,则ccTouchMoved(),ccTouchEnded()不会再接收到消息

b.返回true,则ccTouchMoved(),ccTouchEnded()可以接收到消息


2.Layer 有多层的情况:

1
virtual bool onTouchBegan(CCTouch*pTouch,CCEvent*pEvent);

a.返回false,则本层的onTouchMoved(),onTouchEnded()不会再接收到消息,但是本层之下的其它层会接收到消息

b.返回true,则本层的onTouchMoved(),onTouchEnded()可以接收到消息,但是本层之下的其它层不能再接收到消息


单点触摸简单用法

在Layer中添加如下代码,重写onTouchxxx函数

1
2
3
4
5
6
7
autodispatcher=Director::getInstance()->getEventdispatcher();
autolistener=EventListenerTouchOneByOne::create();
listener->onTouchBegan=CC_CALLBACK_2(GameLayer::onTouchBegan, this );
listener->onTouchMoved=CC_CALLBACK_2(GameLayer::onTouchMoved, this );
listener->onTouchEnded=CC_CALLBACK_2(GameLayer::onTouchEnded, this );
listener->setSwallowtouches( true ); //不向下传递触摸
dispatcher->addEventListenerWithSceneGraPHPriority(listener, this );


listener->setSwallowtouches(true),不向下触摸,简单点来说,比如有两个sprite,A 和 B,A在上B在下(位置重叠),触摸A的时候,B不会受到影响;


listener->setSwallowtouches(false)反之,向下传递触摸,触摸A也等于触摸了B;


多点触摸点单用法(多个Layer获取屏幕事件)

1
2
3
4
5
6
autodispatcher=Director::getInstance()->getEventdispatcher();
autolistener1=EventListenerTouchAllAtOnce::create();
listener1->ontouchesBegan=CC_CALLBACK_2(GameLayer::ontouchesBegan, this );
listener1->ontouchesMoved=CC_CALLBACK_2(GameLayer::ontouchesMoved, this );
listener1->ontouchesEnded=CC_CALLBACK_2(GameLayer::ontouchesEnded, this );
dispatcher->addEventListenerWithSceneGraPHPriority(listener1, this );


或者setTouchEnabled(true),然后重写layer的onTouchsxxx函数


关于eventdispatcher

1) 获取方法

1
autodispatcher=Director::getInstance()->getEventdispatcher();


事件监听器包含以下几种:

触摸事件 (EventListenerTouch)

键盘响应事件 (EventListenerKeyboard)

加速记录事件 (EventListeneracceleration)

鼠标响应事件 (EventListenerMouse)

自定义事件 (EventListenerCustom)


以上事件监听器统一由 _eventdispatcher 来进行管理。


2)优先权

1.优先级越低,越先响应事件

2.如果优先级相同,则上层的(z轴)先接收触摸事件


有两种方式将 事件监听器 listener1 添加到 事件调度器_eventdispatcher 中:

1
2
void Eventdispatcher::addEventListenerWithSceneGraPHPriority(EventListener*listener,Node*node)
void Eventdispatcher::addEventListenerWithFixedPriority(EventListener*listener, int fixedPriority)


代码展开一下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Eventdispatcher::addEventListenerWithSceneGraPHPriority(EventListener*listener,Node*node)
{
CCASSERT(listener&&node, "Invalidparameters." );
CCASSERT(!listener->isRegistered(), "Thelistenerhasbeenregistered." );
if (!listener->checkAvailable())
return ;
listener->setSceneGraPHPriority(node);
listener->setFixedPriority(0);
listener->setRegistered( true );
addEventListener(listener);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Eventdispatcher::addEventListenerWithFixedPriority(EventListener*listener, int fixedPriority)
{
CCASSERT(listener, "Thelistenerhasbeenregistered." );
CCASSERT(fixedPriority!=0, "0priorityisforbiddenforfixedprioritysinceit'susedforscenegraphbasedpriority." );
if (!listener->checkAvailable())
return ;
listener->setSceneGraPHPriority(nullptr);
listener->setFixedPriority(fixedPriority);
listener->setRegistered( true );
listener->setPaused( false );
addEventListener(listener);
}


(1)addEventListenerWithSceneGraPHPriority 的事件监听器优先级是0,而且在 addEventListenerWithFixedPriority 中的事件监听器的优先级不可以设置为 0,因为这个是保留给 SceneGraPHPriority 使用的。


(2)另外,有一点非常重要,FixedPriority listener添加完之后需要手动remove,而SceneGraPHPriority listener是跟node绑定的,在node的析构函数中会被移除。


移除方法

1
dispatcher->removeEventListener(listener);


其实还可以这么用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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->setSwallowtouches( true );
listener->onTouchBegan=[&](Touch*t,Event*e){
//改变贪食蛇移动的方向
int col=t->getLocation().x/32;
int row=t->getLocation().y/32;
int spHeadCol=spHead->getPositionX()/32;
int spHeadRow=spHead->getPositionY()/32;
if ( abs (spHeadCol-col)> abs (spHeadRow-row))
{
if (spHeadCol<col)
{
spHead->m_dir=ENUM_DIR::DIR_RIGHT;
} else
{
spHead->m_dir=ENUM_DIR::DIR_LEFT;
}
}
else
{ if (spHeadRow<row)
{
spHead->m_dir=ENUM_DIR::DIR_UP;
} else
{
spHead->m_dir=ENUM_DIR::DIR_DOWN;
}
}
return true ;
}; //这条语句像不像Java中的匿名接口对象,像不像Objective-C中Block,他有个很华丽的名字lambda,读音为:腊母达,相信未来你会喜欢上这妹子的
//3注册这个侦听到消息分发器中
_eventdispatcher->addEventListenerWithSceneGraPHPriority(listener, this );

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐