在Cocos2dx 3.0版本中,废弃了以往2.x版本的写法,我们先来看一下Layer.h中的一段代码:
1
2
3
4
5
6
7
8
9
10
11
@H_502_32@
|
//单点触摸
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(
|
单点触摸:(即只有注册的Layer才能接收触摸事件)
onTouchBegan
如果返回true:本层的后续Touch事件可以被触发,并阻挡向后层传递
如果返回false,本层的后续Touch事件不能被触发,并向后传递,也就是不会调用
onTouchMoved
简单点来说,如果:
1.Layer 只有一层的情况:
a.返回false,则ccTouchMoved(),ccTouchEnded()不会再接收到消息
b.返回true,则ccTouchMoved(),ccTouchEnded()可以接收到消息
2.Layer 有多层的情况:
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;
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函数
猜你在找的Cocos2d-x相关文章 |