我有一个用Haxe / Flash编写的游戏循环。由于某种原因,它会随着时间的推移而减慢。起初它运行合理,但我的笔记本电脑风扇开始旋转,它变得越来越慢。为什么会这样?
import flash.display.StageDisplayState;
import flash.display.StageScaleMode;
import flash.display.MovieClip;
import flash.display.BitmapData;
import flash.display.Stage;
import flash.events.Event;
import flash.geom.Matrix;
class Driver {
static var scale:Int = 8;
static var stage:Stage
= flash.Lib.current.stage;
static var drv:Driver;
static var mc:MovieClip
= flash.Lib.current;
var x:Int;
var w:Int;
var h:Int;
var bbuf:BitmapData;
var mtx:Matrix;
private function tick() {
x = (x + 1) % Std.int(w/scale);
}
private function draw() {
bbuf.setPixel(x - 1, 10, 0);
bbuf.setPixel(x, 10, 0xff0000);
}
private function flip() {
mc.graphics.beginBitmapFill(bbuf, mtx);
mc.graphics.moveTo(0, 0);
mc.graphics.lineTo(w, 0);
mc.graphics.lineTo(w, h);
mc.graphics.lineTo(0, h);
mc.graphics.endFill();
}
private function OnEnter(e:Event) {
tick();
draw();
flip();
}
public function new() {
mtx = new Matrix();
mtx.scale(scale, scale);
w = stage.stageWidth;
h = stage.stageHeight;
mc = flash.Lib.current;
bbuf = new BitmapData(
Std.int(w/scale), Std.int(h/scale),
false, 0);
if (w % scale != 0) {
trace("width not a multiple of "
+ "scale factor");
return;
}
if (h % scale != 0) {
trace("height not a multiple of "
+ "scale factor");
return;
}
stage.addEventListener(
Event.ENTER_FRAME, OnEnter);
}
static function main() {
drv = new Driver();
}
}
程序显示水平移动屏幕的红点。在一两分钟的过程中,点变得越来越慢。我在两个独立的系统上试过这个(Windows 7,Ubuntu 11)。