Cocos2d-X 学习笔记 22 CCLayer 界面Touch事件处理

Cocos2d-X 学习笔记 22 CCLayer 界面Touch事件处理,第1张

概述Cocos2d 开发中提供了两种touch处理方式,Standard Touch Delegate和 Targeted Touch Delegate方式(参见CCTouchDelegateProtocol.h中源代码),CCLayer默认是采用第一种方式(参见CCLayer的 registerWithTouchDispatcher方法)。在源码中可以看见CCLayer继承了CCTouchDeleg

Cocos2d 开发中提供了两种touch处理方式,Standard touch Delegate和 Targeted touch Delegate方式(参见CCtouchDelegateProtocol.h中源代码),cclayer默认是采用第一种方式(参见cclayer的 registerWithtouchdispatcher方法)。在源码中可以看见cclayer继承了CCtouchDelegate


class CC_DLL cclayer : public CCNode,public CCtouchDelegate,public CCAccelerometerDelegate,public CCKeypadDelegate

cclayer中有一个cctouchesMode 变量用来管理是单点触摸还是多点触摸。cctouchesMode的定义如下:

typedef enum {
kCCtouchesAllAtOnce,
kCCtouchesOneByOne,
} cctouchesMode;

其中kCCtouchesAllAtOnce表示单点触摸,也就是cocos2dx默认的,kCCtouchesOneByOne表示多点触摸。在cclayer.cpp源码文件的构造函数中我们可以看见默认设置:

cclayer::cclayer()
: m_btouchEnabled(false)
,m_bAccelerometerEnabled(false)
,m_bKeypadEnabled(false)
,m_pScripttouchHandlerEntry(NulL)
,m_pScriptKeypadHandlerEntry(NulL)
,m_pScriptAccelerateHandlerEntry(NulL)
,m_ntouchPriority(0)
,m_etouchMode(kCCtouchesAllAtOnce)
{
m_bIgnoreAnchorPointForposition = true;
setAnchorPoint(ccp(0.5f,0.5f));
}

在源码cclayer中就有函数settouchMode用来设置两种模式:

voID cclayer::settouchMode(cctouchesMode mode)
{
if(m_etouchMode != mode)
{
m_etouchMode = mode;

if( m_btouchEnabled)
{
settouchEnabled(false);
settouchEnabled(true);
}
}
}


所以,我们在继承的cclayer类中,只需要调用此函数就可以改变单点还是多点触摸。在源码的cclayer中还有一个变量用来控制是否可以响应用户的触摸变量m_btouchEnabled。要响应触摸还得设置m_btouchEnabled为true。对应也有一个设置函数:

/// istouchEnabled setter
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);
}
}
}
}

可以看出在设置触摸是否可用时,调用了本地的registerWithtouchdispatcher函数:

/// touch and Accelerometer related


voID cclayer::registerWithtouchdispatcher()
{
CCtouchdispatcher* pdispatcher = CCDirector::sharedDirector()->gettouchdispatcher();


// Using LuaBindings
if (m_pScripttouchHandlerEntry)
{
if (m_pScripttouchHandlerEntry->isMultitouches())
{
pdispatcher->addStandardDelegate(this,0);
LUALOG("[LUA] Add multi-touches event handler: %d",m_pScripttouchHandlerEntry->getHandler());
}
else
{
pdispatcher->addTargetedDelegate(this,
m_pScripttouchHandlerEntry->getPriority(),
m_pScripttouchHandlerEntry->getSwallowstouches());
LUALOG("[LUA] Add touch event handler: %d",m_pScripttouchHandlerEntry->getHandler());
}
}
else
{
if( m_etouchMode == kCCtouchesAllAtOnce ) {
pdispatcher->addStandardDelegate(this,0);
} else {
pdispatcher->addTargetedDelegate(this,m_ntouchPriority,true);
}
}
}


可知,根据m_etouchMode 的类型又调用的CCDirector的CCtouchdispatcher变量来注册通知。


所以我们有两种方法可以再自己的cclayer中来响应触摸。

方法1:

settouchMode(kCCtouchesAllAtOnce);//单点触摸
settouchEnabled(true);

settouchMode(kCCtouchesOneByOne);//多点触摸
settouchEnabled(true);

方法二:

CCDirector::sharedDirector()->gettouchdispatcher()->addStandardDelegate(this,0); //单点触摸

CCDirector::sharedDirector()->gettouchdispatcher()->addTargetedDelegate(this,true); //多点触摸

然后我们只需要根据要求来重写delegate函数:

//多点触摸

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,CCEvent *pEvent); //单点 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,CCEvent *pEvent);

总结

以上是内存溢出为你收集整理的Cocos2d-X 学习笔记 22 CCLayer 界面Touch事件处理全部内容,希望文章能够帮你解决Cocos2d-X 学习笔记 22 CCLayer 界面Touch事件处理所遇到的程序开发问题。

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

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

原文地址:https://54852.com/web/1008135.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存