Question

I'm trying to develop an app for iOS using flash CS6. I have imported an image using a loader. I now want to be able to create a duplicate instance of the loaders bitmap data and have been trying:

var my_loader:Loader = new Loader();
        my_loader.load(new URLRequest("cats.jpg"));
        my_loader.scaleX = 0.2;
        my_loader.scaleY = 0.2;
        addChild(my_loader);

        var duplicationBitmap:Bitmap = new Bitmap(Bitmap(my_loader.content).bitmapData);
        duplicationBitmap.x = 300;
        duplicationBitmap.y = 300;
        addChild(duplicationBitmap);

Unfortunately when I test the code it doesn't work. I get the initial loaded image but not the duplicate, I also get an output error of:

TypeError: Error #1009: Cannot access a property or method of a null object reference. at Main()

Any ideas would be greatly appreciated.

Was it helpful?

Solution 2

You can draw the loader on to a BitmapData object when the loader initializes, then simply use it to create as many Bitmap objects as you need when the loader completes.

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.events.Event;

var loaderBitmapData:BitmapData;

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, loaderInitEventHandler);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteEventHandler);
loader.load(new URLRequest("eXO-01.png"));

function loaderInitEventHandler(event:Event):void
{
    loader.contentLoaderInfo.removeEventListener(Event.INIT, loaderInitEventHandler);

    loaderBitmapData = new BitmapData(event.target.width, event.target.height);
    loaderBitmapData.draw(event.target.loader as Loader);
}

function loaderCompleteEventHandler(event:Event):void
{
    loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loaderCompleteEventHandler);

    createBitmaps();
}

function createBitmaps():void
{
    var image1:Bitmap = new Bitmap(loaderBitmapData);
    image1.scaleX = image1.scaleY = 0.2;

    var image2:Bitmap = new Bitmap(loaderBitmapData);
    image2.scaleX = image2.scaleY = 0.4;
    image2.x = image2.y = 100;

    addChild(image1);
    addChild(image2);
}

OTHER TIPS

Bitmap(my_loader.content) is a DisplayObject, not neccessary a Bitmap, that gives you the nullpointer error. For copying bitmapData, you should use BitmapData.clone().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top