欢迎关注公众号:“Cocos Creator 源码讲解”,一起学习。
/* Blinks a Node object by modifying it's visible property
* @class Blink
* @extends ActionInterval
* @param {Number} duration duration in seconds
* @param {Number} blinks blinks in times
* @example
* var action = new cc.Blink(2, 10);
*/
/* 通过修改 Node 对象的可见属性来使其闪烁
* @类眨眼
* @extends ActionInterval
* @param {Number} 持续时间 持续时间(以秒为单位)
* @param {Number} 闪烁 闪烁次数
* @例子
* var action = new cc.Blink(2, 10);
*/
cc.Blink = cc.Class({
name: 'cc.Blink',
extends: cc.ActionInterval,
ctor: function (duration, blinks) {
this._times = 0;
/* 原始状态 */
this._originalState = false;
blinks !== undefined && this.initWithDuration(duration, blinks);
},
/*
* Initializes the action.
* @param {Number} duration duration in seconds
* @param {Number} blinks blinks in times
* @return {Boolean}
*/
/*
* 初始化操作。
* @param {Number} 持续时间 持续时间(以秒为单位)
* @param {Number} 闪烁 闪烁次数
* @return {布尔值}
*/
initWithDuration: function (duration, blinks) {
if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
this._times = blinks;
return true;
}
return false;
},
clone: function () {
var action = new cc.Blink();
this._cloneDecoration(action);
action.initWithDuration(this._duration, this._times);
return action;
},
update: function (dt) {
dt = this._computeEaseTime(dt);
if (this.target && !this.isDone()) {
var slice = 1.0 / this._times;
var m = dt % slice;
this.target.opacity = (m > (slice / 2)) ? 255 : 0;
}
},
/* 设置target */
startWithTarget: function (target) {
cc.ActionInterval.prototype.startWithTarget.call(this, target);
this._originalState = target.opacity;
},
/* 停止action */
stop: function () {
this.target.opacity = this._originalState;
cc.ActionInterval.prototype.stop.call(this);
},
/* 逆向action */
reverse: function () {
var action = new cc.Blink(this._duration, this._times);
this._cloneDecoration(action);
this._reverseEaseList(action);
return action;
}
});
/**
* !#en Blinks a Node object by modifying it's visible property.
* !#zh 闪烁(基于透明度)。
* @method blink
* @param {Number} duration duration in seconds
* @param {Number} blinks blinks in times
* @return {ActionInterval}
* @example
* // example
* var action = cc.blink(2, 10);
*/
/* 创建blink对象 */
cc.blink = function (duration, blinks) {
return new cc.Blink(duration, blinks);
};