Mr.ke
Mr.ke

Reputation: 65

AS3: Is it an faster way to update the bitmap by ActionScript?

-I am making an emulator by AS3,I've nearly finished it,but some efficient problem bothers me.
-By using setPixel method,the redraw seems very slow,I know that it is not the best way to achieve it by AS3.
-I just wonder whether some better way to speed it up?

    for (var a:int = 0; a<8; a++)
{
    color = 0;
    if ( vram&1 ) color = 0x0000FF00;
    setPixel(k, j, color);
    k++;
    vram = vram >> 1;
}

Upvotes: 0

Views: 534

Answers (1)

stalem
stalem

Reputation: 219

I'm not sure how much this will speed things up, but using lock() and unlock() in combination with copyPixels() instead of setPixel() should be faster.

Start by creating a 1x2 BitmapData instance, which you can use as a buffer. Have the left-most pixel at 0x0 and the right-most at 0x0000FF00. That way you could simply copy the appropriate pixel from the buffer.

This way you also remove the overhead of setting the color variable 8 times for each call to the function it resides in.

Here's a general idea of what I mean, don't forget to fill in the appropriate values for alpha if you intend to copy that as well

// Instantiate these once only(!)
// Preferably in the constructor or an init functin
var buffer:BitmapData = new BitmapData(2,1);
var rect:Rectangle = new Rectangle(0,0,1,1);
var p:Point = new Point();

And here's the code

lock();

for(var a:uint = 0; a < 8; a++)
{
    // Select what color to copy, i.e. it's position in the buffer
    rect.x = (vram&1) ? 1 : 0;

    p.x = k; p.y = j;
    copyPixels( buffer, rect, p );
    k++;
    vram = vram >> 1;
}

unlock();

Upvotes: 2

Related Questions