1
votes

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?

1
Loader.content is Bitmap only if you load image files: JPG, PNG, GIF, etc. If you load SWF, the Loader.content refers to main timeline object, which is MovieClip or main document class subclassing MovieClip or Sprite.Organis
Are you actually wanting to convert the loaded SWF to a static image? Or just add the SWF to the display as-is?BadFeelingAboutThis
@BadFeelingAboutThis First off, great name! These swf (which I also have in png and svg as well) are floor plans of a house and my end goal is make them clear as day (png not working out so well) and add listener events so I can zoom in and out of the swf or png or svg.user979331
Ok, then you can simply do container.addChild(LoaderInfo(loadEvent.currentTarget).content); after adding your click listener (regardless of if you're loading a swf or image). Keep using the SWF if it's vector (shape) graphics. Otherwise a PNG will be the same quality assuming it's native resolutionBadFeelingAboutThis
@Organis - consider making your comment an answer for this question so it can get cataloged as an answered question.BadFeelingAboutThis

1 Answers

1
votes

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"));