修复Vector2.zero引起的引用混乱问题
This commit is contained in:
20
source/src/Utils/Timers/ITimer.ts
Normal file
20
source/src/Utils/Timers/ITimer.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
module es {
|
||||
export interface ITimer {
|
||||
context: any;
|
||||
|
||||
/**
|
||||
* 调用stop以停止此计时器再次运行。这对非重复计时器没有影响。
|
||||
*/
|
||||
stop();
|
||||
|
||||
/**
|
||||
* 将计时器的运行时间重置为0
|
||||
*/
|
||||
reset();
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
getContext<T>(): T;
|
||||
}
|
||||
}
|
||||
52
source/src/Utils/Timers/Timer.ts
Normal file
52
source/src/Utils/Timers/Timer.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
module es {
|
||||
export class Timer implements ITimer{
|
||||
public context: any;
|
||||
public _timeInSeconds: number = 0;
|
||||
public _repeats: boolean = false;
|
||||
public _onTime: (timer: ITimer) => void;
|
||||
public _isDone: boolean = false;
|
||||
public _elapsedTime: number = 0;
|
||||
|
||||
public getContext<T>(): T {
|
||||
return this.context as T;
|
||||
}
|
||||
|
||||
public reset() {
|
||||
this._elapsedTime = 0;
|
||||
}
|
||||
|
||||
public stop() {
|
||||
this._isDone = true;
|
||||
}
|
||||
|
||||
public tick(){
|
||||
// 如果stop在tick之前被调用,那么isDone将为true,我们不应该再做任何事情
|
||||
if (!this._isDone && this._elapsedTime > this._timeInSeconds){
|
||||
this._elapsedTime -= this._timeInSeconds;
|
||||
this._onTime(this);
|
||||
|
||||
if (!this._isDone && !this._repeats)
|
||||
this._isDone = true;
|
||||
}
|
||||
|
||||
this._elapsedTime += Time.deltaTime;
|
||||
|
||||
return this._isDone;
|
||||
}
|
||||
|
||||
public initialize(timeInsSeconds: number, repeats: boolean, context: any, onTime: (timer: ITimer)=>void){
|
||||
this._timeInSeconds = timeInsSeconds;
|
||||
this._repeats = repeats;
|
||||
this.context = context;
|
||||
this._onTime = onTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* 空出对象引用,以便在js需要时GC可以清理它们的引用
|
||||
*/
|
||||
public unload(){
|
||||
this.context = null;
|
||||
this._onTime = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
32
source/src/Utils/Timers/TimerManager.ts
Normal file
32
source/src/Utils/Timers/TimerManager.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
module es {
|
||||
/**
|
||||
* 允许动作的延迟和重复执行
|
||||
*/
|
||||
export class TimerManager extends GlobalManager {
|
||||
public _timers: Timer[] = [];
|
||||
|
||||
public update() {
|
||||
for (let i = this._timers.length - 1; i >= 0; i --){
|
||||
if (this._timers[i].tick()){
|
||||
this._timers[i].unload();
|
||||
this._timers.removeAt(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 调度一个一次性或重复的计时器,该计时器将调用已传递的动作
|
||||
* @param timeInSeconds
|
||||
* @param repeats
|
||||
* @param context
|
||||
* @param onTime
|
||||
*/
|
||||
public schedule(timeInSeconds: number, repeats: boolean, context: any, onTime: (timer: ITimer)=>void){
|
||||
let timer = new Timer();
|
||||
timer.initialize(timeInSeconds, repeats, context, onTime);
|
||||
this._timers.push(timer);
|
||||
|
||||
return timer;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user