user979331
user979331

Reputation: 11961

AS3 - TypeError: Error #1034: Type Coercion failed: cannot convert flash.display to flash.display.Bitmap

I am trying to load an swf and display it as a Bitmap.

So far I have been able to load the swf:

loader.load(new URLRequest("assets/floorplan.swf"));
loader.contentLoaderInfo.addEventListener(flash.events.Event.COMPLETE, initPic);

Now on load completion I am looking to take this swf and convert it to Bitmap like so:

public function initPic(loadEvent:flash.events.Event):void
{

    container.addEventListener(MouseEvent.CLICK, zoom);

    bitmapData = Bitmap(LoaderInfo(loadEvent.currentTarget).content).bitmapData;
    image = new Bitmap(bitmapData);
    spImage.addChild(image);
    container.addChild(spImage);

}

However I am getting an error saying TypeError: Error #1034: Type Coercion failed: cannot convert flash.display to flash.display.Bitmap.

What am I doing wrong?

Upvotes: 1

Views: 1044

Answers (1)

Organis
Organis

Reputation: 7316

Loader.content is Bitmap only if you load image files: JPG, PNG, GIF, etc.

If you load a SWF, the Loader.content refers to the main timeline object, which is MovieClip or main document class subclassing MovieClip or Sprite (which cannot be cast to a Bitmap).

Then, Loader is a display object container for the loaded content, thus if only want to scale/position/rotate the loaded graphics, it is a good idea to operate the Loader instance, because accessing content might be unavailable due to security reasons.

Another good idea is to listen to INIT event rather than COMPLETE, because COMPLETE is fired when all the bytes are loaded, while INIT is dispatched later, when loaded content is actually ready.

loader.load(new URLRequest("assets/floorplan.swf"));
loader.contentLoaderInfo.addEventListener(Event.INIT, initPic);

public function initPic(e:Event):void
{
    container.addChild(LoaderInfo(e.currentTarget).loader);
    container.addEventListener(MouseEvent.CLICK, zoom);
}

Furthermore, if your container has no graphics besides the loaded content, you are free to simplify your code as following:

container.addChild(loader);
container.addEventListener(MouseEvent.CLICK, zoom);

loader.load(new URLRequest("assets/floorplan.swf"));

Upvotes: 1

Related Questions