目前小弟我寫Tween的方式
可以用相同的方式來控制rotation . alpha等等
ping-pong . loop之類的模式也可以在擴展之後實現
//使用方式:使用var tween = tween.begin(目標,起始位置,終點位置,花費時間)
//之後在 tween.onFinishCallBack 指定回調就可以了
//tween.onFinishCallBack= function(){cc.log("done");}
var tweenPosition = cc.Class({
extends: cc.Component,
properties: {
time: 1, //Tween完成所花費的時間
timer: 0, //計時器
from: new cc.Vec2(0, 0),//Tween開始的值
to: new cc.Vec2(0, 0), //Tween結束的值
isDone: false,
onFinishCallBack: { //Tween結束之後的回調
default: null,
type: Function
}
},
statics: {
//將target在time秒之內從from移動到to
begin: function (target, from, to, time) {
var tween = target.getComponent(tweenPosition);
if (tween)
tween.destroy();
tween = target.addComponent(tweenPosition);
tween.from = from;
tween.to = to;
tween.time = time;
return tween;
}
},
// use this for initialization
onLoad: function () {
this.node.setPosition(this.from.x, this.from.y);
},
// called every frame, uncomment this function to activate update callback
update: function (dt) {
if (this.isDone) {
this.destroy();
return;
}
this.timer += dt;
this.node.setPosition(
this.from.x + (this.to.x - this.from.x) * (this.timer / this.time),
this.from.y + (this.to.y - this.from.y) * (this.timer / this.time)
);
if (this.timer >= this.time) {
this.isDone = true;
if (this.onFinishCallBack) {
this.onFinishCallBack();
}
}
},
});