Cocos2d-x 学习笔记(11.2) RotateBy RotateTo
1. RotateBy RotateTo
两个旋转方法。RotateBy是在当前角度上旋转设置的角度。RotateTo是直接旋转到设置的角度,方向遵循“就近原则”。两者没有相互继承关系。
1.1 成员变量
RotateBy成员变量:
bool _is3D; Vec3 _deltaAngle; // 设置的角度 Vec3 _startAngle; // 开始的角度
RotateTo成员变量:
bool _is3D; Vec3 _dstAngle; // 设置的角度 Vec3 _startAngle; // 开始的角度 Vec3 _diffAngle; // 实际旋转的角度
1.2 create
RotateBy:
// initWithDuration方法 ActionInterval::initWithDuration(duration)) _deltaAngle.x = _deltaAngle.y = deltaAngle;
RotateTo:
// initWithDuration方法 ActionInterval::initWithDuration(duration)) _dstAngle.x = dstAngleX; _dstAngle.y = dstAngleY;
1.3 startWithTarget
RotateBy:
ActionInterval::startWithTarget(target); if(_is3D) { _startAngle = target->getRotation3D(); } else { _startAngle.x = target->getRotationSkewX(); _startAngle.y = target->getRotationSkewY(); }
_startAngle获取了当前的旋转角度。
RotateTo:
if (_is3D) { _startAngle = _target->getRotation3D(); } else { _startAngle.x = _target->getRotationSkewX(); _startAngle.y = _target->getRotationSkewY(); } calculateAngles(_startAngle.x, _diffAngle.x, _dstAngle.x); calculateAngles(_startAngle.y, _diffAngle.y, _dstAngle.y); calculateAngles(_startAngle.z, _diffAngle.z, _dstAngle.z);
除了获取当前角度,还调用了calculateAngles方法计算_diffAngle。
1.4 "就近原则":RotateTo的calculateAngles方法
首先把开始的角度对正负360度求余:
if (startAngle > 0) { startAngle = fmodf(startAngle, 360.0f); } else { startAngle = fmodf(startAngle, -360.0f); }
然后计算diffAngle:
diffAngle = dstAngle - startAngle;
diffAngle要求【-540<=角度<=+540】,否则不遵循就近原则。
diffAngle的值修改成“就近原则”后计算的新值:
if (diffAngle > 180) { diffAngle -= 360; } if (diffAngle < -180) { diffAngle += 360; }
1.5 upate
RotateBy,我们设置的_deltaAngle就是旋转的角度:
if (_startAngle.x == _startAngle.y && _deltaAngle.x == _deltaAngle.y) { _target->setRotation(_startAngle.x + _deltaAngle.x * time); } else { _target->setRotationSkewX(_startAngle.x + _deltaAngle.x * time); _target->setRotationSkewY(_startAngle.y + _deltaAngle.y * time); }
RotateTo,_diffAngle才是实际旋转的角度:
if (_startAngle.x == _startAngle.y && _diffAngle.x == _diffAngle.y) { _target->setRotation(_startAngle.x + _diffAngle.x * time); } else { _target->setRotationSkewX(_startAngle.x + _diffAngle.x * time); _target->setRotationSkewY(_startAngle.y + _diffAngle.y * time); }