Friday, October 8, 2010

Dynamically load an image using the ActionScript 3 Loader Class

Flex Builder 4 Flash Video


This Image Was Loaded Dynamically


Loading images dynamically gives you the ability to change or update the graphic outside of flash.

The code displays how you can load and position an image in ActionScript 3.0 using the Loader class and the URLRequest and center it on the stage.

var myPic:String = "http://www.yoursite.com/images/myPic.jpg";
var urlRequest:URLRequest = new URLRequest(myPic);
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadImg);
loader.load(urlRequest);
addChild(loader);
 
function loadImg(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;

The UILoader also dynamically loads GIF, PNG and SWF's into the flash player.

ActionScript 3.0 Language and Components Reference

  • URLLoader The URLLoader class downloads data from a URL
  • URLRequest The URLRequest class captures all of the information in a single HTTP request
  • flash.net The whole The flash.net package

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...