Cocos2d-x study notes (11.3) JumpBy JumpTo

1. JumpBy JumpTo

JumpBy, dancing and translation, not only parabolic moves vertically upward, while also translating to the end. JumpTo is JumpBy subclasses.

1.1 create member variables method

JumpBy:

    Vec2 _startPosition; // startWithTarget getPosition provided 
    Vec2 _delta; // create Parameters position disposed 
    a float            _height; // Create Parameter
     int              _jumps; // Create parameters 
    Vec2 _previousPos; // startWithTarget getPosition provided

Jumpto:

    Vec2 _startPosition; // startWithTarget getPosition provided 
    Vec2 _delta; // _endPosition Save _startPosition 
    a float            _height; // Create Parameter
     int              _jumps; // Create parameters 
    Vec2 _previousPos; // startWithTarget getPosition provided 
    Vec2 _endPosition; // create a parameter set position

JumpBy::create(float duration, const Vec2& position, float height, int jumps)

JumpTo::create(float duration, const Vec2& position, float height, int jumps)

1.2 start with target

JumpBy:

ActionInterval::startWithTarget(target);
_previousPos = _startPosition = target->getPosition();

Jumpto:

JumpBy :: start with target (target); 
_delta. set (_endPosition.x - _startPosition.x, _endPosition.y - _startPosition.y);

1.3 update

JumpBy:

First calculate float frac:

float frac = fmodf( t * _jumps, 1.0f );

t * _jumps the progress of the current total number of hops is completed, with the remainder of the value of 1 is the progress of the completion of the ongoing jump.

Next calculate float y:

float y = _height * 4 * frac * (1 - frac);

A jump curve is a quadratic function curve, y = _height * 4 * frac * (1 - frac) is frac is X, Y is a quadratic function equation y established. frac is 1, a jump operation is completed. frac is 0.5, the highest point is moved to the jump operation.

Next:

* + = _delta.y Y T; // ~ delta are disposed end point coordinates create
 a float X = T * _delta.x;

The y coordinates of the current progress of the translational y-coordinate plus a jump of the progress of the current, as the current value y final.

x X direction as far as the progress of the translation, jumping nothing to do with the X direction.

Next is simple:

Vec2 currentPos = _target->getPosition();
Vec2 diff = currentPos - _previousPos;
 _startPosition = diff + _startPosition;
Vec2 newPos = _startPosition + Vec2(x,y);
_target->setPosition(newPos);
_previousPos = newPos;

Move and almost, just before newPos has been calculated well. Here also uses macro definitions to determine CC_ENABLE_STACKABLE_ACTIONS.

Jumpto:

Use JumpBy parent update method, but is different from the _delta To is calculated in the startWithTarget positioin, By directly using positioin we set.

Guess you like

Origin www.cnblogs.com/deepcho/p/cocos2dx-action-jumpby-jumpto.html