Ticore's Blog
以下是用AS 2.0類別繼承MovieClip
以setInerval的方式模擬FPS的功能
為了避免與既有的影格控制函式發生衝突
一併連影格控制函式一起覆寫了
但是使用上仍有不少限制
譬如主要撥放swf的FPS要越高越好
不然用setInterval模擬無法達到高FPS的效果
class MovieClipFPS extends MovieClip {
//
//==================================================
//
private var _fps:Number = 12;
private var intervalId:Number;
private var target:MovieClip;
//
private var _play:Function;
private var _gotoAndPlay:Function;
private var _gotoAndStop:Function;
private var _stop:Function;
private var _nextFrame:Function;
private var _prevFrame:Function;
//
//==================================================
//
public function MovieClipFPS() {
init();
}
public function init():Void {
//覆寫相關影格控制function
this._play = this.play;
this.play = function() {
stopInterval();
startInterval();
};
this._gotoAndPlay = this.gotoAndPlay;
this.gotoAndPlay = function(frame) {
stopInterval();
this._gotoAndPlay(frame);
startInterval();
};
this._gotoAndStop = this.gotoAndStop;
this.gotoAndStop = function(frame) {
stopInterval();
this._gotoAndStop(frame);
};
this._stop = this.stop;
this.stop = function() {
stopInterval();
};
this._nextFrame = this.nextFrame;
this.nextFrame = function() {
stopInterval();
this._nextFrame();
};
this._prevFrame = this.prevFrame;
this.prevFrame = function() {
stopInterval();
this._prevFrame();
};
//
_stop();
target = this;
startInterval();
}
//
//==================================================
//
public function set fps(f:Number):Void {
_fps = f;
if (intervalId != null) {
stopInterval();
startInterval();
}
}
public function get fps():Number {
return _fps;
}
//
//==================================================
//
private function startInterval():Void {
//trace(1000 / _fps);
intervalId = setInterval(run, Math.floor(1000 / _fps), this);
}
private function stopInterval():Void {
clearInterval(intervalId);
intervalId = null;
}
private function run(target):Void {
//trace("run");
//trace(target._currentframe + " : " + target._totalframes);
target._gotoAndStop((target._currentframe) % target._totalframes + 1);
updateAfterEvent();
}
//
//==================================================
//
}
Read more...