在多点触屏的实现中,我们用到了CCLayer中的: void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent); void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent); void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent); void ccTouche
在多点触屏的实现中,我们用到了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上移除、触摸的时候来电话了等。
就可以完成了多点触屏!