cocos2dx之多点触屏

在多点触屏的实现中,我们用到了CCLayer中的:

    void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);
    void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent);
    void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);
    void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent);


在初始化该Layer的时候,记得添加如下代码:

this->setTouchEnabled(true);
void MenuDemo::onEnter() {
    CCLayer::onEnter();
}

上面的onEnter也是关键处,如果不添加的话,就不会在layer收到点击消息!


然后分别实现,这几个方法即可!

void MenuDemo::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent) {
    CCTouch *touch = dynamic_cast<CCTouch *>(pTouches->anyObject()); //一定要添加这行代码,不然就无法正确获取到你鼠标点击的位置。
    CCPoint point = touch->locationInView();
    CCPoint GLPoint = CCDirector::sharedDirector()->convertToGL(point);//转化成你的view坐标

    CCLabelTTF *label = CCLabelTTF::create("abc", "Arial", 24);
    label->setColor(ccc3(255, 100, 0));
    label->setPosition(CCPointMake(GLPoint.x  , GLPoint.y));
    this->addChild(label, 4);
    CCLOG("label->x: %f, label->y: %f", label->getPositionX(), label->getPositionY());
    CCLOG("ccTouchBegan\n");
}


还有就是采用addTargetedDelegate来实现,在初始化layer的时候,初始化:

CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);

实现ccTouchBegan:

bool GameDemoManager::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) { 
	CCPoint	point = pTouch->getLocation();
	//CCPoint GLpoint = CCDirector::sharedDirector()->convertToGL(point);
	CCLabelTTF *label = CCLabelTTF::create("www", "Arial", 32);
	label->setColor(ccc3(255,0 ,0));
	label->setPosition(ccp(point.x, point.y));
	this->addChild(label, 1);
	return true;
}

ccTouchesCancelled和ccTouchCancelled函数很少用,在接到系统中断通知,需要取消触摸事件的时候才会调用此方法。如:应用长时间无响应、当前view从window上移除、触摸的时候来电话了等。

就可以完成了多点触屏!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值