Nice one, Guy.

kp

On Jul 11, 2007, at 9:51 PM, Guy Watson - FlashGuru LTD wrote:

Double Buffering was necessary in Flash 8, but in Flash 9 with Actionscript 3 the speed difference between setting pixels on a bitmapData object that is on the display list and settings pixels on a bitmapData object that isnt on the display can be reduced quite significantly with the introduction of the bitmapData.lock() and bitmapData.unlock() methods.

To decrease the difference in speed some more simply hide the Bitmap container of the bitmapData object you are setting pixels on, until you have finished, for example:

package
{
    import flash.display.Sprite
    import flash.display.Bitmap
    import flash.display.BitmapData
    import flash.utils.getTimer

    public class Test extends Sprite
    {
        public function Test()
        {
            var bmp1:BitmapData = new BitmapData(500,500,true,0)
            var bmp2:BitmapData = bmp1.clone()
            var bmp3:BitmapData = bmp2.clone()

            var holder1:Bitmap = new Bitmap(bmp1)
            addChild(holder1)

            var holder2:Bitmap = new Bitmap(bmp2)

            var start:int
            var i:uint

            start=getTimer()
            bmp1.lock()
            holder1.visible=false
            for(i=0;i<1000000;++i)
            {
bmp1.setPixel(Math.random()*500, Math.random() *500,Math.random()*0xFFFFFFFF)
            }
            holder1.visible=false
            bmp1.unlock()
            trace(getTimer()-start)

            start=getTimer()
            for(i=0;i<1000000;++i)
            {
bmp2.setPixel(Math.random()*500,Math.random() *500,Math.random()*0xFFFFFFFF)
            }
            trace(getTimer()-start)

            start=getTimer()
            for(i=0;i<1000000;++i)
            {
bmp3.setPixel(Math.random()*500,Math.random() *500,Math.random()*0xFFFFFFFF)
            }
            trace(getTimer()-start)
        }

    }
}

I get the following results in the output window:

1160
1366
1143

Which is a bit different from the results you get when you dont use the lock() and unlock() methods, and dont hide the holder bitmap:

package
{
    import flash.display.Sprite
    import flash.display.Bitmap
    import flash.display.BitmapData
    import flash.utils.getTimer

    public class Test extends Sprite
    {
        public function Test()
        {
            var bmp1:BitmapData = new BitmapData(500,500,true,0)
            var bmp2:BitmapData = bmp1.clone()
            var bmp3:BitmapData = bmp2.clone()

            var holder1:Bitmap = new Bitmap(bmp1)
            addChild(holder1)

            var holder2:Bitmap = new Bitmap(bmp2)

            var start:int
            var i:uint

            start=getTimer()
            for(i=0;i<1000000;++i)
            {
bmp1.setPixel(Math.random()*500,Math.random() *500,Math.random()*0xFFFFFFFF)
            }
            trace(getTimer()-start)

            start=getTimer()
            for(i=0;i<1000000;++i)
            {
bmp2.setPixel(Math.random()*500,Math.random() *500,Math.random()*0xFFFFFFFF)
            }
            trace(getTimer()-start)

            start=getTimer()
            for(i=0;i<1000000;++i)
            {
bmp3.setPixel(Math.random()*500,Math.random() *500,Math.random()*0xFFFFFFFF)
            }
            trace(getTimer()-start)
        }

    }
}

Which outputs:

1377
1356
1130

Kind Regards
Guy
-----------------------------
www.flashguru.co.uk


_______________________________________________
osflash mailing list
[email protected]
http://osflash.org/mailman/listinfo/osflash_osflash.org

_______________________________________________
osflash mailing list
[email protected]
http://osflash.org/mailman/listinfo/osflash_osflash.org

Reply via email to