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);


	}
}





 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值