CocosCreator 源码cc.RotateTo详解

/*
 * 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);
};

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值