现在的位置: 首页 > 综合 > 正文

cocos2dx 3.1.1官方demo阅读-ActionManagerTest【动作管理】

2017年12月22日 ⁄ 综合 ⁄ 共 3468字 ⁄ 字号 评论关闭
文章目录

这个例子中共包括了5个Action。

Layer* createActionManagerLayer(int nIndex)
{
    switch(nIndex)
    {
        case 0: return new CrashTest();
        case 1: return new LogicTest();
        case 2: return new PauseTest();
        case 3: return new StopActionTest();
        case 4: return new ResumeTest();
    }

    return NULL;
}

注意ActionManager类里面的主要方法,可耻地加点蹩脚的中文吧。

    /** Adds an action with a target. 
     If the target is already present, then the action will be added to the existing target.
     If the target is not present, a new instance of this target will be created either paused or not, and the action will be added to the newly created target.
     When the target is paused, the queued actions won't be 'ticked'.
     */
	/*对一个目标添加action。paused用于标记一系列actions是否不会被触发,true不会被触发,false会被触发。
	如果目标已经存在,action会添加到那个已经存在的目标。
	如果目标不存在,不管paused的值,一个新的实例将会被创建(target),动作将会被添加到这个target上。
	*/
    void addAction(Action *action, Node *target, bool paused);	 

    /** Removes all actions from all the targets.
    */
    void removeAllActions();         

    /** Removes all actions from a certain target.
     All the actions that belongs to the target will be removed.
     */
	//从目标移除action
    void removeAllActionsFromTarget(Node *target);

    /** Removes an action given an action reference.
    */
	//指定action并移除
    void removeAction(Action *action);

    /** Removes an action given its tag and the target */
	//通过标记移除action
    void removeActionByTag(int tag, Node *target);

    /** Gets an action given its tag an a target
     @return the Action the with the given tag
     */
    Action* getActionByTag(int tag, const Node *target) const;

    /** Returns the numbers of actions that are running in a certain target. 
     * Composable actions are counted as 1 action. Example:
     * - If you are running 1 Sequence of 7 actions, it will return 1.
     * - If you are running 7 Sequences of 2 actions, it will return 7.
     */
	//获取正在运行的action,特别注意,返回的是Sequences的个数。
    ssize_t getNumberOfRunningActionsInTarget(const Node *target) const;

    /** @deprecated use getNumberOfRunningActionsInTarget() instead */
    CC_DEPRECATED_ATTRIBUTE inline ssize_t numberOfRunningActionsInTarget(Node *target) const { return getNumberOfRunningActionsInTarget(target); }

    /** Pauses the target: all running actions and newly added actions will be paused.
    */
    void pauseTarget(Node *target);

    /** Resumes the target. All queued actions will be resumed.
    */
    void resumeTarget(Node *target);
    
    /** Pauses all running actions, returning a list of targets whose actions were paused.
     */
    Vector<Node*> pauseAllRunningActions();
    
    /** Resume a set of targets (convenience function to reverse a pauseAllRunningActions call)
     */
    void resumeTargets(const Vector<Node*>& targetsToResume);

PauseTest中的pause(3秒,官方注释写的5秒)效果

1>向grossin添加一个不立即执行的动作

2>注册一个3秒后执行的计时器

3>取消上面注册的计时器,相当于只执行一次。

4>恢复grossin的action

void PauseTest::onEnter()
{
    //
    // This test MUST be done in 'onEnter' and not on 'init'
    // otherwise the paused action will be resumed at 'onEnter' time
    //
    ActionManagerTest::onEnter();
    

    auto l = Label::createWithTTF("After 5 seconds grossini should move", "fonts/Thonburi.ttf", 16.0f);
    addChild(l);
    l->setPosition( Vec2(VisibleRect::center().x, VisibleRect::top().y-75) );
    
    
    //
    // Also, this test MUST be done, after [super onEnter]
    //
    auto grossini = Sprite::create(s_pathGrossini);
    addChild(grossini, 0, kTagGrossini);
    grossini->setPosition(VisibleRect::center() );
    
    auto action = MoveBy::create(1, Vec2(150,0));

    auto director = Director::getInstance();
    //对grossin添加一个action,不立即执行
    director->getActionManager()->addAction(action, grossini, true);

    //注册一个定时器,3秒执行一次
    schedule( schedule_selector(PauseTest::unpause), 3); 
}

void PauseTest::unpause(float dt)
{
    //取消定时器,效果相当于停止了三秒
    unschedule( schedule_selector(PauseTest::unpause) );
    auto node = getChildByTag( kTagGrossini );
    auto director = Director::getInstance();
    //恢复grossin的动作
    director->getActionManager()->resumeTarget(node);
}

通过Tag控制action,主要语句。

   pSequence->setTag(kTagSequence);
   sprite->stopActionByTag(kTagSequence);

抱歉!评论已关闭.