欢迎关注公众号:“Cocos Creator 源码讲解”,一起学习。
/* Scales a Node object to a zoom factor by modifying it's scale property.
* @warning This action doesn't support "reverse"
* @class ScaleTo
* @extends ActionInterval
* @param {Number} duration
* @param {Number} sx scale parameter in X
* @param {Number} [sy] scale parameter in Y, if Null equal to sx
* @example
* // It scales to 0.5 in both X and Y.
* var actionTo = new cc.ScaleTo(2, 0.5);
*
* // It scales to 0.5 in x and 2 in Y
* var actionTo = new cc.ScaleTo(2, 0.5, 2);
*/
/* 通过修改 Node 对象的缩放属性,将其缩放至缩放系数。
* @警告此操作不支持“反向”
* @类ScaleTo
* @extends ActionInterval
* @param {Number} 持续时间
* @param {Number} X 中的 sx 比例参数
* @param {Number} [sy] Y 中的比例参数,如果 Null 等于 sx
* @例子
* //X 和 Y 方向都缩放至 0.5。
* var actionTo = new cc.ScaleTo(2, 0.5);
*
* //x 缩放为 0.5,Y 缩放为 2
* var actionTo = new cc.ScaleTo(2, 0.5, 2);
*/
cc.ScaleTo = cc.Class({
name: 'cc.ScaleTo',
extends: cc.ActionInterval,
ctor: function (duration, sx, sy) {
/* 初始化参数,当前scalex 和 scaley;开始action之前的 scalex,scaley, 最终需要到达的scalex 和y */
this._scaleX = 1;
this._scaleY = 1;
this._startScaleX = 1;
this._startScaleY = 1;
this._endScaleX = 0;
this._endScaleY = 0;
this._deltaX = 0;
this._deltaY = 0;
sx !== undefined && cc.ScaleTo.prototype.initWithDuration.call(this, duration, sx, sy);
},
/*
* Initializes the action.
* @param {Number} duration
* @param {Number} sx
* @param {Number} [sy=]
* @return {Boolean}
*/
/*
* 初始化操作。
* @param {Number} 持续时间
* @param {Number} sx
* @param {数字} [sy=]
* @return {布尔值}
*/
initWithDuration: function (duration, sx, sy) { //function overload here
if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
this._endScaleX = sx;
this._endScaleY = (sy != null) ? sy : sx;
return true;
}
return false;
},
/* 复制action */
clone: function () {
var action = new cc.ScaleTo();
this._cloneDecoration(action);
action.initWithDuration(this._duration, this._endScaleX, this._endScaleY);
return action;
},
/* 设置target+ */
startWithTarget: function (target) {
cc.ActionInterval.prototype.startWithTarget.call(this, target);
this._startScaleX = target.scaleX;
this._startScaleY = target.scaleY;
this._deltaX = this._endScaleX - this._startScaleX;
this._deltaY = this._endScaleY - this._startScaleY;
},
/* 根据dt时时更新scale */
update: function (dt) {
dt = this._computeEaseTime(dt);
if (this.target) {
this.target.scaleX = this._startScaleX + this._deltaX * dt;
this.target.scaleY = this._startScaleY + this._deltaY * dt;
}
}
});
/**
* !#en Scales a Node object to a zoom factor by modifying it's scale property.
* !#zh 将节点大小缩放到指定的倍数。
* @method scaleTo
* @param {Number} duration
* @param {Number} sx scale parameter in X
* @param {Number} [sy] scale parameter in Y, if Null equal to sx
* @return {ActionInterval}
* @example
* // example
* // It scales to 0.5 in both X and Y.
* var actionTo = cc.scaleTo(2, 0.5);
*
* // It scales to 0.5 in x and 2 in Y
* var actionTo = cc.scaleTo(2, 0.5, 2);
*/
/* 创建sceleTo对象 */
cc.scaleTo = function (duration, sx, sy) { //function overload
return new cc.ScaleTo(duration, sx, sy);
};