2

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.

  • 1
    I fixed all performance issues in my own [mobile] game by simplifying the Graphics. My source was an FXG Files; which I render offscreen to a bitmap and then use copyPixels to create the game board. By simplifying the graphics I cut the file size of each graphic down by ~1/3 and all performance issues went away. You could have a similar issue here. – JeffryHouser Nov 28 '12 at 16:34
  • Just at a short overview: redrawing all graphics on EnterFrame is wrong. You should redraw only on changes. – Adrian Pirvulescu Feb 15 '13 at 13:19

0 Answers0