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 (4)

Share:
  • Facebook
  • Google Bookmarks
  • Faves
  • Twitter
  • Yahoo! Bookmarks

Leave a Reply

Coding Color | 2009 All Rights Reserved.