Cocos2d-x 多点触摸

Posted jhcelue

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Cocos2d-x 多点触摸相关的知识,希望对你有一定的参考价值。

多点触摸的实现步骤与单点触摸类似,setTouchEnabled(true)开启触摸功能,注冊触摸事件,重载多点触摸函数:ccTouchesBegan(開始)、ccTouchesMoved(移动)、ccTouchesEnded(结束)、ccTouchesCancelled(系统中断取消触摸事件),每一个多点触摸函数都能够通过遍历整个CCSet来获得全部的触点。

 

程序代码:

头文件增加触摸重载函数:

              virtual void registerWithTouchDispatcher(void);
	virtual void ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);
	virtual void ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);
	virtual void ccTouchesEnded(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);
	virtual void ccTouchesCancelled(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);

 

cpp文件增加:

setTouchEnabled(true);

 

定义实现这些函数:

void HelloWorld::registerWithTouchDispatcher()
{
	CCDirector *pdir = CCDirector::sharedDirector();
	pdir->getTouchDispatcher()->addStandardDelegate(this, 0);
}
void HelloWorld::ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{
	CCSetIterator iter = pTouches->begin();
	for (; iter != pTouches->end(); iter++)
	{
		CCTouch* pTouch = (CCTouch*)(*iter);
		
		CCPoint location = pTouch->getLocation();
		CCLOG("ccTouchesBegan x: %f, y: %f", location.x, location.y);

		
	}
}

void HelloWorld::ccTouchesEnded(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{
	CCSetIterator iter = pTouches->begin();
	for (; iter != pTouches->end(); iter++)
	{
		CCTouch* pTouch = (CCTouch*)(*iter);

		CCPoint location = pTouch->getLocation();
		CCLOG("ccTouchesEnded x: %f, y: %f", location.x, location.y);


	}
}
void HelloWorld::ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{
	CCSetIterator iter = pTouches->begin();
	for (; iter != pTouches->end(); iter++)
	{
		CCTouch* pTouch = (CCTouch*)(*iter);

		CCPoint location = pTouch->getLocation();
		CCLOG("ccTouchesMoved x: %f, y: %f", location.x, location.y);


	}
}
void HelloWorld::ccTouchesCancelled(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{
	CCSetIterator iter = pTouches->begin();
	for (; iter != pTouches->end(); iter++)
	{
		CCTouch* pTouch = (CCTouch*)(*iter);

		CCPoint location = pTouch->getLocation();
		CCLOG("x: %f, y: %f", location.x, location.y);


	}
}





 

以上是关于Cocos2d-x 多点触摸的主要内容,如果未能解决你的问题,请参考以下文章

WPF中的多点触摸事件

cocos2d-x之多点触碰初试

处理子层 Cocos2d-x 的触摸

如何在 Swift 3 中多点触摸 UICollectionViewCell

在我的应用程序中禁用多指触摸[重复]

cocos2d-x 源代码分析 : EventDispatcherEventListenerEvent 源代码分析 (新触摸机制,新的NotificationCenter机制)