Frage

I am new to cocos2d-x, now i am developing a game in xcode using cocos2d-x. In my game I want my sprite rotate on the button press but it is rotating on button release. following is the code i used. please help me to find this.

CCMenuItemImage *rotate = CCMenuItemImage::create(
                                                      "rotateround.png",
                                                      "rotateround.png",
                                                      this,
                                                      menu_selector(PlayScene::rotate) );
rotate->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 70,70) );

void gamescene::rotate()
{    
anim1=CCAnimation::create();
anim1->addSpriteFrameWithFileName("rotate.png");
anim1->addSpriteFrameWithFileName("rotate1.png");
anim1->addSpriteFrameWithFileName("rotate2.png");
anim1->addSpriteFrameWithFileName("rotate3.png");
anim1->addSpriteFrameWithFileName("rotate4.png");
anim1->setLoops(3);
anim1->setDelayPerUnit(0.7f);
man->runAction(CCAnimate::create(anim1));
}
War es hilfreich?

Lösung

This is the way that CCMenu and its items work - you get the callback when the user releases the touch. If you want to detect when the user touches down, tou should use CCControlButton.

Example :

    CCControlButton *gzk = CCControlButton::create(CCScale9Sprite::create("res/FB.png"));
    gzk->setAdjustBackgroundImage(false);
    gzk->setTag(TAG_FB);
    gzk->setPosition(ccp(31, 517));
    gzk->addTargetWithActionForControlEvents(this, cccontrol_selector(MyClass::touch), CCControlEventTouchDownInside); // this line is the most interesting for you
    addChild(gzk, 10);

In the example the callback will be called when the user touches the button. CCControlButton is generally better - you can addTargetWitchAction for different events with different callbacks.

Edit : follow up on a comment - "now it is keep on rotating even though I release the button.how to stop the action while release the button."

This is because you run the action when the button is touched down, but it is not tied to it in any way. Meaning that you have to specifficaly add another action and callback to your button, for example :

gzk->addTargetWithActionForControlEvents(this, cccontrol_selector(MyClass::endTouch), CCControlTouchUpInside);
gzk->addTargetWithActionForControlEvents(this, cccontrol_selector(MyClass::endTouch), CCControlTouchUpOutside);

And the the endTouch() should look something like this :

void MyClass::endTouch() {
    man->stopAllActions(); //to stop all actions running on a node
    ------ or ------
    man->stopAction(anim1); //if you kept the reference to the action
    ------ or ------
    man->stopActionbyTag(tag); // if you assigned a tag to your action
}

Hope this helps!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top