cocos2d-x中关于touch事件的响应

cocos2d-x中关于touch事件的响应,第1张

概述http://blog.sina.com.cn/s/blog_bfabaf830101g82g.html 一、touch事件响应分为单点触摸响应和多点触摸响应。     单点触摸响应需要重载的方法:   virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);   virtual void ccTouchMoved(CCTouch *


http://blog.sina.com.cn/s/blog_bfabaf830101g82g.HTML

一、touch事件响应分为单点触摸响应和多点触摸响应。

单点触摸响应需要重载的方法:

virtual bool cctouchBegan(CCtouch *ptouch,CCEvent *pEvent);
virtual voID cctouchmoved(CCtouch *ptouch,CCEvent *pEvent);

virtual voID cctouchended(CCtouch *ptouch,CCEvent *pEvent);
virtual voID cctouchCancelled(CCtouch *ptouch,226)"> 多点触摸需要重载的方法:

virtual voID cctouchesBegan(CCSet *ptouches,CCEvent *pEvent);
virtual voID cctouchesMoved(CCSet *ptouches,CCEvent *pEvent);
virtual voID cctouchesEnded(CCSet *ptouches,CCEvent *pEvent);
virtual voID cctouchesCancelled(CCSet *ptouches,226)"> 二、单点触摸本质上需要调用的方法,即设置touch的代理
CCDirector::sharedDirector()->gettouchdispatcher()->addTargetedDelegate(this,-129,true);

多点触摸本质上需要调用的方法

CCDirector::sharedDirector()->gettouchdispatcher()->addStandardDelegate(this,-129);

所以,只要调用了以上方法,就可以实现触摸响应,就不再需要调用settouchEnabled(true);

三、settouchEnabled的方法实现

voID cclayer::settouchEnabled(bool enabled)
{
if (m_btouchEnabled != enabled)
{
m_btouchEnabled = enabled;
if (m_bRunning)
{
if (enabled)
{
this->registerWithtouchdispatcher();
}
else
{
// have problems?
CCDirector::sharedDirector()->gettouchdispatcher()->removeDelegate(this);
}
}
}
}

这段代码最主要的一句this->registerWithtouchdispatcher();

这个方法中就是调用第二点中提到的两个delegate添加方法。

所以,咱们实现touch响应的话,需要重载 registerWithtouchdispatcher()方法,并在这个方法中添加实现

voID HelloWorld::registerWithtouchdispatcher()
{
CCDirector::sharedDirector()->gettouchdispatcher()->addStandardDelegate(this,226)"> //CDirector::sharedDirector()->gettouchdispatcher()->addTargetedDelegate(this,true);
}

由此,实现touch事件响应需要做的 *** 作:

1.重载registerWithtouchdispatcher()并实现

2.settouchEnabled(true);

3.重载touch或者touches系列方法

四、在cclayer的registerWithtouchdispatcher()中有关于m_etouchMode的判断,并且有settouchMode()方法,参数有两种:kCCtouchesOneByOne和 kCCtouchesAllAtOnce,分别代表单点触摸和多点触摸

当touchMode为kCCtouchesOneByOne时,调用的是addTargetedDelegate方法;当touchMode为kCCtouchesAllAtOnce时,调用的是addStandardDelegate方法

所以,“重载registerWithtouchdispatcher()并实现”可以由settouchMode()方法来替换

总结

以上是内存溢出为你收集整理的cocos2d-x中关于touch事件的响应全部内容,希望文章能够帮你解决cocos2d-x中关于touch事件的响应所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1063053.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存