Thursday, March 3, 2011

Embedding Resources/Images/JPG/PNG/SWF with AS3

If you are wondering how you can add sexy JPG or fantastic PNG or "copied/borrowed" SWF to your boring SWF without editing the FLA, then below is an example on how to do it. 

In this example, I will show you how to add an image to stage after using the embed statement.



package {
  public class EmbedResourcesExample1 {
    [Embed(source="../images/HelloWorld.png", mimeType="image/png")]
    private var MyImage:Class;

    public function EmbedResourcesExample1() : void {
      var image:Bitmap = new MyImage();
      addChild(image);
    }
  }
}

This next example, I will show you how to embed a SWF to your main SWF.

package {
  public class EmbedResourcesExample2 {
    [Embed(source="../images/HelloWorld.swf", symbol="hello")]
    private var MySWF:Class;

    public function EmbedResourcesExample2() : void {
      var mySWF:Sprite = new MySWF();
      addChild(mySWF);
    }
  }
}

And there you have it.  With the above samples, you can now successfully add JPGs or PNGs or SWFs to your main SWF file.

Related Links:
Error transcoding
Embed Fonts


1 comment:

  1. u can do so much more in just two lines png bitmap jpeg , its all good !, try
    https://github.com/ammarahmedkhan/Display.as ;-)

    ReplyDelete