cocos2d-x ActionManager控制精灵的暂停运动

运行环境:vs2010 cocos2d-x2.2 

cocos2d-x ActionManager控制精灵的暂停运动_第1张图片


运行环境:vs2010 cocos2d-x2.2 
ActionManager看名字就知道是对整个Action的管理,我们可以通过单例的 CCDirector来获得 ActionManager,通过ActionManager我们可以控制某个精灵对象移动。
  • 1.先设置一个精灵的移动:
                        CCAnimation* pSpriteAnimation = CCAnimation::create();
                        pSpriteAnimation->setLoops(-1);
                        pSpriteAnimation->setDelayPerUnit(0.1f);
                        pSpriteAnimation->addSpriteFrameWithFileName(  "loading_02.png"  );
                        pSpriteAnimation->addSpriteFrameWithFileName(  "loading_03.png"  );
                        pSpriteAnimation->addSpriteFrameWithFileName(  "loading_04.png"  );
                        CCAnimate* animate = CCAnimate::create(pSpriteAnimation);
                        pSprite->runAction(animate);
  • 2.加入一个控件就是图片上显示的pause,这里点击一下就会调用3中所写的暂停。

                        CCControlButton* controlButton = CCControlButton::create( "pause"  ,  "Arial" ,52);
                        controlButton->setPosition(size.width - controlButton->getContentSize().width/2,size.height -
                                    controlButton->getContentSize().height/2);
                        controlButton->addTargetWithActionForControlEvents(  this  ,
                                    cccontrol_selector(HelloWorld::testPause),CCControlEventTouchDown);
  • 3.ActionManager控制精灵的暂停与继续播放示例代码:
            CCDirector* director = CCDirector::sharedDirector();
            
              if  (!isPause)
            {
                        director->getActionManager()->pauseTarget(  this  ->getChildByTag(1));
                        isPause=!isPause;
            }  else   if  (isPause)
            {
                        director->getActionManager()->resumeTarget(  this  ->getChildByTag(1));
                        isPause=!isPause;
            }
我们看pauseTarget的声名为:
      /** Pauses the target: all running actions and newly added actions will be paused.
    */
     void  pauseTarget(CCObject *pTarget); 
对传入的对象进行暂停。

代码链接:http://pan.baidu.com/share/link?shareid=215292333&uk=2768417218

你可能感兴趣的:(cocos2d-x ActionManager控制精灵的暂停运动)