【文章主要是记录自己的学习过程】
本文参考引用如下文章:
http://www.cnblogs.com/haogj/p/3784896.html
http://www.bubuko.com/infodetail-1135295.html
http://book.51cto.com/art/201209/355846.htm
1、触摸事件与鼠标事件的区别:
触摸事件时可以是多点的,而鼠标事件总是单点的,如果程序并不需要使用多点触摸的功能,而仅仅是传统的单点交互,那么鼠标事件就可以满足。
2、
//添加触屏委托,重新此方法
virtual void onEnter()
//移除监听事件,防止内存溢出。
virtual void onExit()
例:
void ::onExit()
{
_eventDispatcher->removeEventListener(this);
Layer::onExit();
}
(自己在写的时候一直都是在init()子函数内绑定触摸事件,没有移除监听事件)
3、
假设Layer1的ZOrder大于Layer2的ZOder,则
Layer1和Layer2的触摸事件回调函数调用情况如下
Layer1 setSwallow: false false true true
Layer1 touchBegan ret: false true false true
--------------------------------------------------------------
Layer1 touchMoved no yes no yes
Layer2 touchBegan yes yes yes no
总结:
只有Layer1的touchBegan返回true时才会执行Layer1的touchMove事件是
只有当Layer1的swallow为true并且Layer1的toucheBegan返回true时Layer2的touchBegan才不会执行。
4、触摸事件响应
virtual bool onTouchBegan(Touch *touch, Event *unused_event);
virtual void onTouchMoved (Touch *touch, Event *unused_event);
virtual void onTouchEnded(Touch *touch, Event *unused_event);
auto touchListener = EventListenerTouchOneByOne::create();
touchListener ->setSwallowTouches(true); //根据需要(解释如上)
touchListener->onTouchBegan = CC_CALLBACK_2(WuziqiOnlinePlayLayer::onTouchBegan, this);
touchListener->onTouchMoved = CC_CALLBACK_2(WuziqiOnlinePlayLayer::onTouchMoved, this);
touchListener->onTouchEnded = CC_CALLBACK_2(WuziqiOnlinePlayLayer::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
根据需要添加相应的函数。
bool ::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *unused_event)
{
return true; //根据需要(解释如上)
}