COCOS2D API 之 BaseAction

Actions

Actions are like orders given to any CCNode object. These actions usually modify some of the object's attributes like positionrotationscale, etc. If these attributes are modified during a period of time, they are CCIntervalAction actions, otherwise they are CCInstantAction actions.

For example, the CCMoveBy action modifies the position property during a period of time, hence, it is a subclass of CCIntervalAction.

Example:

# Move a sprite 50 pixels to the right, and 10 pixels to the top over 2 seconds.
[sprite runAction: [CCMoveBy actionWithDuration:2 position:ccp(50,10)]];

The CCIntervalAction actions have some interesting properties:

  • They can be accelerated using the time-altered actions
    • CCEaseIn
    • CCEaseOut
    • CCEaseInOut
    • CCSpeed
    • Etc. (See the EaseActionsTest.m example for more info)
  • All the relative actions (the ones ending in 'By') and some absolute actions (the ones ending in 'To') have a reverse action ([action reverse]) that executes the action in the opposite direction.

    You can pause/resume all actions by using the CCActionManager:

    # Pause actions
    [[CCActionManager sharedManager ] pauseAllActionsForTarget:sprite ] ;
     
    # resume actions
    [[CCActionManager sharedManager ] resumeAllActionsForTarget:sprite ] ;

    Basic Actions

    Basic actions are the ones that modify basic properties like:

    Example:

    CGSize s = [[CCDirector sharedDirector] winSize];
     
    id actionTo = [CCMoveTo actionWithDuration: 2 position:ccp(s.width-40, s.height-40)];
    id actionBy = [CCMoveBy actionWithDuration:2  position: ccp(80,80)];
     
    [sprite1 runAction: actionTo];
    [sprite2 runAction:actionBy];

    Reverse Action

    Almost all actions have the reverse method implemented. Basically it creates a new action with the reverse behavior.

    Example:

    id move = [CCMoveBy actionWithDuration:2  position: ccp(80,80)];
     
    id move_reverse = [move reverse];

    The move_reverse action will be a CCMoveBy action of duration 2, but with the position value of ccp(-80,-80).

posted on 2012-03-02 16:17  jackbutler  阅读(112)  评论(0编辑  收藏  举报

导航