AS3 load an image from the library

I’m essentially posting this, as a response to search terms that people frequently use , which eventually direct them to an old posting on loading images. Unfortunately, the posting does not cover loading an image directly from the flash library, but the method is fairly simple. When you import a bitmap into Flash, you can modify that bitmap and use it in a variety of ways, the simple usage is to display it via the linkage properties.

Open up the Flash IDE, presumably CS4 but CS3 will do.

Select File->Import->Import to Library..

Select your image to import.
Now navigate to your library.

Double click on the bitmap asset to activate the Bitmap Properties panel:

Navigate down to the linkage properties and select Export For ActionScript. You will notice that the Export in frame 1 checkbox will automatically be selected and the Class textfield will be highlighted. Go ahead and add an identifier string of your choice in the Class textfield and Select OK. Essentially, Flash has generated a linked class by the specified identifier automatically, which will allow you to have access to the Bitmap. All you have left to do is create a base class for your .fla and display your bitmap.

Open up your text editor and create a class like so:

package{
 
  import flash.display.Sprite;
  import flash.display.Bitmap;
 
  public class Main extends Sprite{
 
  private var imageSprite:Sprite;
 
  public function Main(){
   
    imageSprite = new Sprite();//create an image container
    addChild(imageSprite);//add it to the display list
    displayImageFromLibrary();
   
  }
 
 
 
  public function displayImageFromLibrary():void
  {
               //instantiate the bitmap in the library by its identifier
    var libraryImage:Bitmap = new Bitmap(new kfc(0,0));

               //place the image in the image container
    imageSprite.addChild(libraryImage);
  }
 
 
 
 
 
  }
 
}

Save the class as Main.as and make sure its in the same folder as the fla.
Navigate back to the Flash Ide and open the properties panel. Enter Main in the class textfield.

Once the program’s document class has been associated with the .fla we can now compile the program, Select-Control Test Movie. If all went well the desired results should have been achieved.

And there you have it.
Photo by Chris Brennan.
Download Source:
AS3 load Image from library (803)

3 thoughts on “AS3 load an image from the library

  • July 9, 2012 at 8:34 am
    Permalink

    hi..
    I’m new to as3..
    I have an external XML file and a single image and I would load it using as3. On the stage is a Movieclip that calls the xml file via as3.

    Here’s how it’s done the xml file:

    and this is the code that calls the as3 xml file:

    var myXMLLoader: URLLoader = new URLLoader ();
    myXMLLoader.load (new URLRequest (“image_load.xml”));

    the question is, how to proceed?

    thank you in advance …

    Reply
    • August 15, 2012 at 4:10 pm
      Permalink

      Are you asking me how to do it, or telling me how to do it?

      Reply
  • March 6, 2013 at 1:33 am
    Permalink

    Many, many thanks for your post. I have Googled lots of time but couldn’t figure out the prob. Your post helped me finally.
    Best Wishes

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: