May 30, 2009

Load an image (jpeg, png, gif) or a swf using ActionScript 3.0

Here is an example that illustrate how you can upload an image or a swf in your Flash ActionScript 3.0 project .

ActionScript 3.0 code:

// create the MovieClip that will hold the object loaded var _mc:MovieClip = new MovieClip(); addChild(_mc); // create the URLRequest with the path to the object you want to load var request:URLRequest = new URLRequest("image.jpg"); var loader = new Loader(); // create the Loader object loader.load(request); loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoadProgress); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete); loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError); // call this function when the object is loading function onLoadProgress(event:ProgressEvent):void {     // display the percentage loaded     trace(int(event.bytesLoaded/event.bytesTotal*100)+"%");     // display the bytes loaded and the total number of bytes     trace(event.bytesLoaded+":"+event.bytesTotal); } // call this function when the object is loaded function onLoadComplete(event:Event):void { // duplicate and resize a loaded image (doesn't work for swf) // ----->     var objectDuplicate:Bitmap = new Bitmap(loader.content.bitmapData.clone());     objectDuplicate.width = 100;     objectDuplicate.height = 100;     objectDuplicate.smoothing = true;     _mc.addChild(objectDuplicate); // ----->     _mc.addChild(loader); // attach object to MovieClip } // call this function if the path to the object that you want to load is not valid function onLoadError(event:Event):void {         trace("Error! The URL was not found."); }

May 5, 2009

How to trigger an event with ActionScript 3.0 when the sizes of a swf’s stage are modified

This is very useful when you try to create a “liquid” flash application.

ActionScript 3.0 code:

//The next 2 lines are not mandatory. //Use them if you whant to keep the original sizes. stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; // Other values: //TOP, BOTTOM, LEFT, RIGHT, TOP_RIGHT // BOTTOM_LEFT, BOTTOM_RIGHT stage.addEventListener(Event.RESIZE, resizeHandler); function resizeHandler(event:Event):void {         trace(stage.stageWidth);         trace(stage.stageHeight);         trace("An event has been triggered."); }