CocosCreator 源码cc.TintTo详解

/* Tints a Node that implements the cc.NodeRGB protocol from current tint to a custom one.

 * @warning This action doesn't support "reverse"

 * @class TintTo

 * @extends ActionInterval

 * @param {Number} duration

 * @param {Number} red 0-255

 * @param {Number} green  0-255

 * @param {Number} blue 0-255

 * @example

 * var action = new cc.TintTo(2, 255, 0, 255);

 */

/* 将实现 cc.NodeRGB 协议的节点从当前色调设置为自定义色调。

 * @警告此操作不支持“反向”

 * @类TintTo

 * @extends ActionInterval

 * @param {Number} 持续时间

 * @param {Number} 红色 0-255

 * @param {Number} 绿色 0-255

 * @param {Number} 蓝色 0-255

 * @例子

 * var action = new cc.TintTo(2, 255, 0, 255);

 */

cc.TintTo = cc.Class({

    name: 'cc.TintTo',

    /* 继承自cc.ActionInterval */

    extends: cc.ActionInterval,

    /* 构造函数 */

    ctor: function (duration, red, green, blue) {

        /* 初始化参数 color的参数,to 和 from 都是空白 */

        this._to = cc.color(0, 0, 0);

        this._from = cc.color(0, 0, 0);



        /* 假如第二个参数 传递的是 Color类型,则获取对应的色值,进行赋值 */

        if (red instanceof cc.Color) {

            blue = red.b;

            green = red.g;

            red = red.r;

        }

        /* opacity 参数不为空的时候,进行赋值 调用父类 ActionInterval进行初始化 */

        blue !== undefined && this.initWithDuration(duration, red, green, blue);

    },



    /*

     * Initializes the action.

     * @param {Number} duration

     * @param {Number} red 0-255

     * @param {Number} green 0-255

     * @param {Number} blue 0-255

     * @return {Boolean}

     */

    /* 

     * 初始化操作。

     * @param {Number} 持续时间

     * @param {Number} 红色 0-255

     * @param {Number} 绿色 0-255

     * @param {Number} 蓝色 0-255

     * @return {布尔值}

      */

    initWithDuration: function (duration, red, green, blue) {

        /* 一定会调用,设置to的rgb参数 */

        if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {

            this._to = cc.color(red, green, blue);

            return true;

        }

        return false;

    },

    /* 复制 TintTo 这个action*/

    clone: function () {

        /* 创建一个 TintTo action */

        var action = new cc.TintTo();

        /* 对action进行赋值 */

        this._cloneDecoration(action);

        var locTo = this._to;

        /* 初始化时间和opacity参数 */

        action.initWithDuration(this._duration, locTo.r, locTo.g, locTo.b);

        /* 返回当前new的action */

        return action;

    },

    /* 初始化target */

    startWithTarget: function (target) {

        cc.ActionInterval.prototype.startWithTarget.call(this, target);

        /* 开始变化的color */

        this._from = this.target.color;

    },

    /* 每一帧都会update */

    update: function (dt) {

        /*     /* 计算easetime */

        /* dt 间隔时间  */

        dt = this._computeEaseTime(dt);

        /* 声明locFrom 为 开始变化的color,声明 locTo 为to的 color */

        var locFrom = this._from, locTo = this._to;

        /* 每一帧都时时更新当前的rgb,根据时间的变化进行更新比例 */

        if (locFrom) {

            this.target.color = cc.color(

                locFrom.r + (locTo.r - locFrom.r) * dt,

                locFrom.g + (locTo.g - locFrom.g) * dt,

                locFrom.b + (locTo.b - locFrom.b) * dt);

        }

    }

});



/**

 * !#en Tints a Node that implements the cc.NodeRGB protocol from current tint to a custom one.

 * !#zh 修改颜色到指定值。

 * @method tintTo

 * @param {Number} duration

 * @param {Number} red 0-255

 * @param {Number} green  0-255

 * @param {Number} blue 0-255

 * @return {ActionInterval}

 * @example

 * // example

 * var action = cc.tintTo(2, 255, 0, 255);

 */

/* 创建 tintTo 对象 */

cc.tintTo = function (duration, red, green, blue) {

    return new cc.TintTo(duration, red, green, blue);

};

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值