I have a game loop written in Haxe/Flash. For some reason it slows down over time. At first it runs reasonably, but my laptop fan starts spinning up and it gets slower and slower. Why would this happen?
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();
}
}
The program shows a red dot moving across the screen horizontally. Over the course of a minute or two the dot gets slower and slower. I have tried this on two separate systems (Windows 7, Ubuntu 11).
Try to clear graphics before beginBitmapFill. I found same topic but did not find in documentation that beginBitmapFill may leak.