响应事件整理

【文章主要是记录自己的学习过程】

本文参考引用如下文章:

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; //根据需要(解释如上)
}

  

 

转载于:https://www.cnblogs.com/pureyes-cyl/p/7066214.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值