c++xcode5cocos2d-xcocos2d-x-2.x

Stop current animation when press release the button up


I am creating a game in xcode using cocos2d-x. I am new to cocos2d-x.

I created two button using CCControlButton for jump & slide. And i created animation for run & slide with same sprite name called man.

For run i created in init() & for slide i created in separate function called rotate.when i press slide button, slide animation is running & when i release run animation is running.

when i hold slide button slide animation should keep slide & when i release slide button, slide animation should stop & run animation should run, but when release the slide button, jump button is not working, because i gave a condition as only if(man->numberofrunningaction()==1) it should jump.

When i press slide, the number of actions is increasing. Anyone please help me to find solution.

I am using following code jump, slide anim & run anim..

   void PlayScene::rotate()
   {
  anim1=CCAnimation::create();
  anim1->addSpriteFrameWithFileName("11.png");
  anim1->addSpriteFrameWithFileName("11.png");
  anim1->addSpriteFrameWithFileName("11.png");
  anim1->setLoops(4);
  anim1->setDelayPerUnit(0.05f);
  man->runAction(CCAnimate::create(anim1));
  man->setPosition(ccp(winwsize/5.5,winhsize/2.2));
  CCRotateBy *rot=CCRotateBy::create(1, 0.000000001);
  man->runAction(rot); 
  }

 init()
 man=CCSprite::create();
 man->retain();

 anim=CCAnimation::create();
 anim->retain();

 anim->addSpriteFrameWithFileName("1.png");
 anim->addSpriteFrameWithFileName("2.png");
 anim->addSpriteFrameWithFileName("3.png");
 anim->addSpriteFrameWithFileName("4.png");
 anim->addSpriteFrameWithFileName("5.png");
 anim->addSpriteFrameWithFileName("6.png");
 anim->addSpriteFrameWithFileName("7.png");
 anim->addSpriteFrameWithFileName("8.png");
 anim->addSpriteFrameWithFileName("9.png");
 anim->setLoops(-1);
 anim->setDelayPerUnit(0.05f);
 man->runAction(CCAnimate::create(anim));
 man->setPosition(ccp(winwsize/5.5,winhsize/2.2));
 this->addChild(man,1);

 creating button for jump & slide.

  CCControlButton *rotate = CCControlButton::create(CCScale9Sprite::create("jump1.png"));
 rotate->setAdjustBackgroundImage(false);
 rotate->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 70,70) );
 rotate>addTargetWithActionForControlEvents(this,cccontrol_selector
  (PlayScene::jumpfun),CCControlEventTouchDown);
 addChild(rotate, 3);

CCControlButton *jumpbutton=CCControlButton::create(CCScale9Sprite::create("slide2.png"));
jumpbutton->setAdjustBackgroundImage(false);
jumpbutton->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 250,70) );    

jumpbutton->addTargetWithActionForControlEvents(this,
cccontrol_selector(PlayScene::rotate),CCControlEventTouchDown);
jumpbutton->addTargetWithActionForControlEvents(this, cccontrol_selector
(PlayScene::buttonup),CCControlEventTouchUpInside);
     addChild(jumpbutton, 3);

 void PlayScene::buttonup()
{
anim1->setLoops(0);
 }

Solution

  • Set tag to identify your animations and stop animation by using stopActionByTag().

    e.g.

    CCAnimate *an =   CCAnimate::create(animation);
    an->setTag(10);
    man->runAction(an);
    

    To stop animation -

    man->stopActionByTag(10);
    

    Create macro for animation tags.

    May this help you.