0

为什么在 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);
}

我做错了什么,还有什么要做的?有没有更简单的方法来做到这一点???

4

2 回答 2

0

您只需要添加一次精灵。您可以保留全局引用或使用其标签检索精灵。此外,您可能会错误地计算触摸位置。尝试这个

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;
}
于 2013-07-16T11:18:50.170 回答
-1

spriteWithFile中不存在cocos2d2.0.4
所以你能根据最新版本更新代码吗COCOCS2D

于 2014-04-30T06:33:20.823 回答