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

スプライトを追加する必要があるのは 1 回だけです。グローバル参照を保持するか、そのタグを使用してスプライトを取得できます。また、タッチ位置を正しく計算していない可能性があります。これを試して

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 に答える