Cocos2d-x study notes (11) MoveBy MoveTo

1. MoveBy MoveTo

Methods are of two translational node, MoveBy is moved from the current position. By MoveTo is a subclass of the world is moving to coordinate positions.

1.1 create member variables and methods

The main MoveBy member variables and methods create major statement:

_is3D BOOL; 
Vec3 _positionDelta; // end disposed opposite the starting point coordinate
Vec3 _startPosition; // start coordinate
Vec3 _previousPosition; // once the coordinates setPosition

ActionInterval::initWithDuration(duration));
_positionDelta = deltaPosition;
_is3D = true;

The main MoveTo member variables and methods create major statement:

Vec3 _endPosition; the end of the world coordinate system coordinates // set 
BOOL _is3D; 
Vec3 _positionDelta; // set of world coordinates into relative coordinates
Vec3 _startPosition; // start coordinates
Vec3 _previousPosition; // on a setPosition coordinates

ActionInterval :: initWithDuration (DURATION); _endPosition
= position;

1.2 startWithTarget way

MoveBy:

ActionInterval::startWithTarget(target);
_previousPosition = _startPosition = target->getPosition3D();

MoveTo:

MoveBy::startWithTarget(target);
_positionDelta = _endPosition - target->getPosition3D();

1.3 update method

Method MoveBy update the main logic:

_target->setPosition3D(_startPosition + _positionDelta * time)

The update is MoveTo parent MoveBy the update.

MoveBy update method CC_ENABLE_STACKABLE_ACTIONS:

#if CC_ENABLE_STACKABLE_ACTIONS
        Vec3 currentPos = _target->getPosition3D();
        Vec3 diff = currentPos - _previousPosition;
        _startPosition = _startPosition + diff;
        Vec3 newPos =  _startPosition + (_positionDelta * t);
        _target->setPosition3D(newPos);
        _previousPosition = newPos;
#else
        _target->setPosition3D(_startPosition + _positionDelta * t);
#endif

This macro defines the default is 1, is conditional compilation.

Conditional compilation process is the first part of the compiler compiles the code. If the macro conditions are met, the compiler will compile the code, otherwise, the compiler will ignore this code does not compile.

After opening macro definition, when a plurality of operation acting in the same node, the node movement pattern of the combined effect of a plurality of actions.

Diff first deviation amount calculating a current position by the position and operation of this last set, recalculates the initial coordinates using the amount of deviation, such that the node before another operation on the basis of the motion.

If only one operation, the amount of deviation is 0, start coordinate will not change.

1.4 Other

When using MoveTo, set a different relationship with the Node Node initial position of runAction statement has, it may become the MoveTo MoveBy.

code show as below:

// 错误
MoveTo *myAction = MoveTo::create(5, Vec2(50,50));
sp->runAction(myAction);
sp->setPosition(Vec2(50,50));
this->addChild(tom);
// 正确
MoveTo *myAction = MoveTo::create(5, Vec2(50,50));
sp->setPosition(Vec2(50,50)); sp->runAction(myAction); this->addChild(tom);

The reason is that runAction calls to MoveBy of startWithTarget:

_positionDelta = _endPosition - target->getPosition3D(); // MoveTo

First runAction then set the initial position, the above _endPosition equal _positionDelta, final coordinates became the starting coordinates relative coordinates, not the result we want. 

Guess you like

Origin www.cnblogs.com/deepcho/p/cocos2dx-action-moveby-moveto.html