/*
* Rotates a Node object to a certain angle by modifying its angle property. <br/>
* The direction will be decided by the shortest angle.
* @class RotateTo
* @extends ActionInterval
* @param {Number} duration duration in seconds
* @param {Number} dstAngle dstAngle in degrees.
* @example
* var rotateTo = new cc.RotateTo(2, 61.0);
*/
/*
* 通过修改 Node 对象的角度属性将其旋转到特定角度。 <br/>
* 方向将由最短角度决定。
* @类RotateTo
* @extends ActionInterval
* @param {Number} 持续时间 持续时间(以秒为单位)
* @param {Number} dstAngle dstAngle(以度为单位)。
* @例子
* varrotateTo = new cc.RotateTo(2, 61.0);
*/
cc.RotateTo = cc.Class({
name: 'cc.RotateTo',
/* 继承自cc.ActionInterval */
extends: cc.ActionInterval,
statics: {
_reverse: false,
},
ctor: function (duration, dstAngle) {
/* 开始的angle角度 */
this._startAngle = 0;
/* 最终的角度 */
this._dstAngle = 0;
/* 当前的角度 */
this._angle = 0;
/* 初始化参数,开始角度和end角度 */
dstAngle !== undefined && this.initWithDuration(duration, dstAngle);
},
/*
* Initializes the action.
* @param {Number} duration
* @param {Number} dstAngle
* @return {Boolean}
*/
/* 调用父类的初始化方法,初始化动作时间参数 */
initWithDuration: function (duration, dstAngle) {
if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
this._dstAngle = dstAngle;
return true;
}
return false;
},
/* 复制Action */
clone: function () {
var action = new cc.RotateTo();
this._cloneDecoration(action);
action.initWithDuration(this._duration, this._dstAngle);
return action;
},
/* 设置 target */
startWithTarget: function (target) {
cc.ActionInterval.prototype.startWithTarget.call(this, target);
/* 取余数,假如大于360度 */
let startAngle = target.angle % 360;
/* 假如是逆向【顺时针】旋转到某个angle,假如reverse,角度90变成-90 */
let angle = cc.RotateTo._reverse ? (this._dstAngle - startAngle) : (this._dstAngle + startAngle);
if (angle > 180) angle -= 360;
if (angle < -180) angle += 360;
/* 全局变量赋值, */
this._startAngle = startAngle;
this._angle = cc.RotateTo._reverse ? angle : -angle;
},
reverse: function () {
cc.logID(1016);
},
/* 每一帧执行 */
update: function (dt) {
dt = this._computeEaseTime(dt);
if (this.target) {
/* 更新angle,开始的angle + dt对应的angle旋转 */
this.target.angle = this._startAngle + this._angle * dt;
}
}
});
/**
* !#en
* Rotates a Node object to a certain angle by modifying its angle property. <br/>
* The direction will be decided by the shortest angle.
* !#zh 旋转到目标角度,通过逐帧修改它的 angle 属性,旋转方向将由最短的角度决定。
* @method rotateTo
* @param {Number} duration duration in seconds
* @param {Number} dstAngle dstAngle in degrees.
* @return {ActionInterval}
* @example
* // example
* var rotateTo = cc.rotateTo(2, 61.0);
*/
// 创建一个对象
cc.rotateTo = function (duration, dstAngle) {
return new cc.RotateTo(duration, dstAngle);
};