I have developed an action game with Flex 4.6.0 and I am having an issue with the drawing part. Actually, during a few days, I thought the problem was the calculation on the physics part, but after a lot of optimizations, I am sure I have a drawing issue.
Let me explain how the code looks like.
In the game loop I have:
protected function onEnterFrame():void {
runPhysics();
drawScene();
}
Inside the runPhysics
function I do a lot of calculation, but as I said, I am sure it's optimized.
Inside drawScene
I have:
protected function drawScene():void {
drawFixedBackground( fixedBitmap );
drawPeriodicBackground( periodicBitmap1 );
drawPeriodicBackground( periodicBitmap2 );
drawLevel();
drawCar();
gamePanel.graphics.clear();
gamePanel.graphics.beginBitmapFill(screenBuffer, null, false, true);
gamePanel.graphics.drawRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
gamePanel.graphics.endFill();
}
The drawFixedBackground
function is quite simple. I just use the copyPixels
function to draw in the entire screen.
The other functions depend on the position of my car and my camera, so I have to scale and translate them before drawing.
I have a global BitmapData
called screenBuffer
in which I draw other BitmapData I have already in memory. For example:
protected function drawPeriodicBackground( bitmap:BitmapData ):void {
var m:Matrix = new Matrix();
m.scale( ... );
m.translate( ... );
screenBuffer.draw( bitmap, m);
}
protected function drawLevel():void {
var m:Matrix = new Matrix();
for(var i:int=0; i<NUMBER_OF_BOARDS; i++) {
m.scale( ... );
m.translate( ... );
screenBuffer.draw(boardBitmap, m);
}
}
protected function drawCar():void {
var m:Matrix = new Matrix();
m.scale( ... );
m.translate( ... );
screenBuffer.draw(carBitmap, m);
}
In this code, gamePanel
is a spark Group
component.
I said I'm sure the physics is not the issue because if I comment out all drawing functions but the drawCar, my car runs nicely, colliding with the invisible boards and keeping the CPU usage quite low (25%). On the other hand, with all drawing functions active, my CPU usage goes to 140% and my car starts lagging in some computers.
Can anyone help me? Am I doing anything wrong?
Thanks in advance.