C++ 如何使此文本标签移动到新的触摸位置并删除Cocos2d-x中的旧标签?
我需要做到以下几点: 当我点击时,“移动”出现在点击的位置 当我再次单击时:旧的“移动”消失,新的“移动”出现在新位置。(不起作用) 这是我当前的代码:C++ 如何使此文本标签移动到新的触摸位置并删除Cocos2d-x中的旧标签?,c++,cocos2d-iphone,cocos2d-x,C++,Cocos2d Iphone,Cocos2d X,我需要做到以下几点: 当我点击时,“移动”出现在点击的位置 当我再次单击时:旧的“移动”消失,新的“移动”出现在新位置。(不起作用) 这是我当前的代码: void MainGame::ccTouchesBegan(CCSet* pTouches, CCEvent* pEvent) { CCSetIterator itr; CCLabelTTF* labelMove = CCLabelTTF::create("Move", "Arial", 24); removeChild(la
void MainGame::ccTouchesBegan(CCSet* pTouches, CCEvent* pEvent)
{
CCSetIterator itr;
CCLabelTTF* labelMove = CCLabelTTF::create("Move", "Arial", 24);
removeChild(labelMove);
for(itr = pTouches->begin(); itr!=pTouches->end(); itr++)
{
CCTouch* thisTouch = (CCTouch*)*itr;
CCPoint labelPosition = thisTouch->getLocation();
labelMove->setPosition(labelPosition);
addChild(labelMove);
}
}
目前,所有发生的是,一个新的“移动”出现在屏幕上的点击位置每次,但旧的都留在屏幕上。在创建新标签之前,是否应该让“removeChild(labelMove)”删除旧标签
谢谢 如果您在CCTouchesBegind方法之外创建此标签会更好。在这种情况下,您不必每次用户触摸屏幕时都删除子项。。。你只需更新它的位置 如果你不能做到这一点,我建议你给标签一个标签,然后删除孩子的标签。。。像这样
void MainGame::ccTouchesBegan(CCSet* pTouches, CCEvent* pEvent)
{
CCSetIterator itr;
removeChildByTag(1);
CCLabelTTF* labelMove = CCLabelTTF::create("Move", "Arial", 24);
labelMove->setTag(1);
for(itr = pTouches->begin(); itr!=pTouches->end(); itr++)
{
CCTouch* thisTouch = (CCTouch*)*itr;
CCPoint labelPosition = thisTouch->getLocation();
labelMove->setPosition(labelPosition);
addChild(labelMove);
}
}