Wednesday, February 1, 2012

Actionscript 3.0 load image from external or library


1) To load a image from external in ActionScript 3.0
(The disadvantage of loading an external file is that you need to upload the flash and the external file both, or it will not play)
    import flash.display.*;
    import flash.events.*;
    import flash.net.URLRequest;
    import flash.geom.*;
    import flash.utils.Timer;
     public function loadBitmap(bitmapFile:String) {
            var loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingDone);
            var request:URLRequest = new URLRequest(bitmapFile);
            loader.load(request);
        }
     public function loadingDone(event:Event):void {
            // create new image to hold loaded bitmap
            var image:Bitmap = Bitmap(event.target.loader.content);
            var pieceWidth:Number = image.width/2.;
            var pieceHeight:Number = image.height/2.;
}

To load an image myimage.jpg, call function loadBitmap("myimage.jpg");
2. To load a image from flash library:
Open Flash CS5. Click import and import to library.
Right click image to get Bitmap Properties:
check box: Export for ActionScript and Export in frame 1. Set class name, for example: penguin2.
Using following code ro create new image to hold the imported image
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    var bmp:BitmapData = new penguin2(0,0);
    var    image:Bitmap = new Bitmap(bmp);   

No comments:

Post a Comment