The following examples show how you can dynamically load an image, calculate its width and height, and center the image on the Stage in ActionScript 2.0 and ActionScript 3.0 using the MovieClipLoader class (ActionScript 2.0) and the Loader class (ActionScript 3.0).
Full code after the jump.
The following example shows how you can load and position an image in ActionScript 2.0 using the MovieClipLoader class and the onLoadInit event handler.
// ActionScript 2.0 var url:String = "http://www.helpexamples.com/flash/images/image2.jpg"; var movieClip:MovieClip = createEmptyMovieClip("movieClip", 0); var listenerObj:Object = new Object(); listenerObj.onLoadInit = function (target_mc:MovieClip):Void { target_mc._x = (Stage.width - target_mc._width) / 2; target_mc._y = (Stage.height - target_mc._height) / 2; } var movieClipLoader:MovieClipLoader = new MovieClipLoader(); movieClipLoader.addListener(listenerObj); movieClipLoader.loadClip(url, movieClip);
The following example shows how you can load and position an image in ActionScript 3.0 using the Loader class and the complete event on the Loader object’s contentLoaderInfo property.
// ActionScript 3.0 var url:String = "http://www.helpexamples.com/flash/images/image2.jpg"; var urlRequest:URLRequest = new URLRequest(url); var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete); loader.load(urlRequest); addChild(loader); function loader_complete(evt:Event):void { var target_mc:Loader = evt.currentTarget.loader as Loader; target_mc.x = (stage.stageWidth - target_mc.width) / 2; target_mc.y = (stage.stageHeight - target_mc.height) / 2; }
7 Responses to Positioning a dynamically loaded image in ActionScript 2.0 and ActionScript 3.0 using the MovieClipLoader class and Loader class
Leave a Reply Cancel reply
Recent Posts
- Getting started with the TLFTextField class in ActionScript 3.0 and Flash CS5
- Adding tick marks to a Slider control in Flash using ActionScript 3.0
- Creating a vertical Slider control in Flash using ActionScript 3.0
- Creating a custom context menu item in Flash using ActionScript 3.0
- Rotating a Sprite object around its x-axis in Flash using ActionScript 3.0 and Flash Player 10
- Detecting when the user changes the color in a ColorPicker control in Flash using ActionScript 3.0
- Getting the currently selected color as a hexadecimal value on a ColorPicker control in Flash using ActionScript 3.0
- Toggling the text field on the ColorPicker control in Flash using ActionScript 3.0
- Creating a vertical Slider control in Flash using ActionScript 3.0
- Setting the number of columns on a ColorPicker control in Flash using ActionScript 3.0
Categories
- Bitmap (1)
- Components (72)
- Button (19)
- CheckBox (2)
- ColorPicker (6)
- ComboBox (1)
- DataGrid (8)
- FLVPlayback (7)
- Label (9)
- ProgressBar (2)
- Slider (3)
- TextArea (1)
- TextInput (7)
- UILoader (7)
- ContextMenu (1)
- Embed (4)
- ExternalInterface (2)
- Flex (7)
- Font (2)
- General (5)
- Graphics (2)
- JSFL (14)
- Loader (3)
- LoadVars (3)
- Microphone (1)
- migration (12)
- MovieClip (1)
- MovieClipLoader (1)
- Sound (2)
- TextField (1)
- TLFTextField (1)
- TransitionManager (1)
- Tween (1)
- Uncategorized (1)
- URLLoader (4)
- URLVariables (1)
- Video (1)
- XML (2)
Advertising
" RT @OReillyMedia: #Ebook Deal/Day: Learning JavaScript Design Patterns -
$11.99
(Save 50%)
http://t.co/ilcmSDv6 " — pdehaan


Hey guys, this works great when I view the exported swf. but when I put it in an html page on a server, it gets off centered. is this because it’s centering before the image loads? or does it possibly have something to do w/ my embed method?
btw, i’m using the as2 method
This is great but what if you want to center the image somwhere other than the center of the stage? Say at an x cordinate?
Chris,
Then set the
xand/oryproperty to whatever coordinates you want. I think I randomly chose to position them in the middle of the stage because it was a bit different in ActionScript 2.0 vs ActionScript 3.0.Peter
I am using a button to load an image into a movie clip so how can I use this code to center the image in the movie clip? This code successfully loads the image into the movieclip:
btn1.addEventListener(MouseEvent.CLICK, img1);
function img1(event:Event):void {
var i =new Loader();
i.load(new URLRequest(“paintings/musicroom.jpg”));
ldr1.addChild(i)
}
Chris,
This is good example but I need to set a preloader with this image and I am confuse how to set it.
regards,
Aleem
how can i modify the code of AS2 for multiple images display?