在多点触屏的实现中,我们用到了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);
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; }