为什么在cocos2dx中拖动精灵这么难!在我的touchesbegan方法中这样做
void HelloWorld::ccTouchesBegan(cocos2d::CCSet* touches, cocos2d::CCEvent* event){
CCSprite *splash = CCSprite::spriteWithFile("splash3.png");
CCTouch* pTouch = (CCTouch*)(touches->anyObject());
CCPoint location = pTouch->locationInView();
location = CCDirector::sharedDirector()->convertToGL(location);
splash->setPosition(ccp(location.x,location.y));
this->addChild(splash,5);
}
void HelloWorld::ccTouchesMoved(cocos2d::CCSet* touches, cocos2d::CCEvent* event){
CCSprite *splash = CCSprite::spriteWithFile("splash3.png");
CCTouch* pTouch = (CCTouch*)(touches->anyObject());
CCPoint location = pTouch->locationInView();
location = CCDirector::sharedDirector()->convertToGL(location);
splash->setPosition(ccp(location.x,location.y));
this->addChild(splash,5);
}
我做错了什么,还有什么要做的?并且有更简单的方法吗???
答案 0 :(得分:0)
您只需要添加一次精灵。您可以保留全局引用或使用其标记检索sprite。此外,您可能错误地计算触摸位置。试试这个
void HelloWorld::ccTouchesBegan(cocos2d::CCSet* touches, cocos2d::CCEvent* event){
CCSprite *splash = CCSprite::spriteWithFile("splash3.png");
CCPoint location = getPositionFromTouches(touches, event);
splash->setPosition(ccp(location.x,location.y));
this->addChild(splash,5, 100);
}
void HelloWorld::ccTouchesMoved(cocos2d::CCSet* touches, cocos2d::CCEvent* event){
CCSprite *splash = (CCSprite*) getChildByTag(100);
CCPoint location = getPositionFromTouches(touches, event);
splash->setPosition(ccp(location.x,location.y));
}
CCPoint HelloWorld::getPositionFromTouches(CCSet* _touches, CCEvent* event) {
CCArray *allTouches = CCArray::create();
CCSetIterator it;
for( it = _touches->begin(); it != _touches->end(); it++)
{
allTouches->addObject((CCTouch *)*it);
}
//CCArray *allTouches = getAllTouchesFromSet(_touches);
CCTouch* fingerOne = (CCTouch *)allTouches->objectAtIndex(0);
CCPoint pointOne = CCDirector::sharedDirector()->convertToUI(fingerOne->getLocationInView());
CCPoint location = _armadilloSingleton->convertToNodeSpace(pointOne);
return location;
}
答案 1 :(得分:-1)
spriteWithFile
中没有{p> cocos2d2.0.4
那么请您根据COCOCS2D
的最新版本更新代码