As the saying goes: You cannot quit. I don't need the skills I have learned, but I am new to them when I keep it long. My colleagues today asked me how to reposition the pictures in flash on four corners when I change the size of the browser window. It took nearly a quarter of a second to recall that the stage had a resize event. The Code is as follows: 1. encapsulate the image loading Logic first.
package {import flash.display.Sprite;import flash.display.Loader;import flash.display.LoaderInfo;import flash.net.URLRequest;import flash.events.Event;import flash.display.Bitmap;import flash.events.IOErrorEvent;import flash.system.LoaderContext;public class LoadImage extends Sprite {private var _imgWidth:int;private var _imgHeight:int;public function LoadImage(url:String,imgWidth:int=380,imgHeight:int=305) {this._imgWidth = imgWidth;this._imgHeight = imgHeight;var _request:URLRequest = new URLRequest(url);var _loader:Loader = new Loader();var _lc:LoaderContext = new LoaderContext(true);_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadComplete);_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,loadIO_Error);_loader.load(_request,_lc);}private function loadComplete(e:Event):void{//trace("loadComplete");var li:LoaderInfo = e.currentTarget as LoaderInfo;var bmp:Bitmap = li.content as Bitmap;bmp.height = _imgHeight;bmp.width = _imgWidth;addChild(bmp);}private function loadIO_Error(e:IOErrorEvent):void{trace("load error!");}}}
2. Main Document class
Package {import flash. display. movieclip; import flash. events. event; import flash. display. stagealign; import flash. display. stagescalemode; public class resizedemo extends movieclip {private VaR _ top_left: LoadImage; // the picture on the left is private VaR _ top_right: LoadImage; // the picture on the right is private VaR _ bottom_left: LoadImage; // Private VaR _ bottom_right: LoadImage; // Private VaR _ center: LoadImage; // Private VaR _ width: int; // The width of the stage private VaR _ Height: int; // The height of the Stage Public Function resizedemo () {// constructor codeif (stage) {Init ();} else {stage. addeventlistener (event. added_to_stage, init) ;}} private function Init (E: event = NULL) {stage. removeeventlistener (event. added_to_stage, init); stage. addeventlistener (event. resize, resizehandler); stage. align = stagealign. top_left; stage. scalemode = stagescalemode. no_scale; // load the image _ top_left = new LoadImage ("top_left.jpg", 100,150); _ top_right = new LoadImage ("top_right.jpg", 100,150 ); _ bottom_left = new LoadImage ("bottom_left.jpg", 100,150); _ bottom_right = new LoadImage ("bottom_right.jpg", 100,150); _ center = new LoadImage ("center.jpg", 200,300 ); addchild (_ top_left); addchild (_ top_right); addchild (_ bottom_left); addchild (_ bottom_right); addchild (_ center); adjustposition ();} private function resizehandler (E: event = NULL) {adjustposition () ;}// adjust the position private function adjustposition () {_ width = stage. stagewidth; _ Height = stage. stageheight; trace ("_ width =", _ width); trace ("_ Height =", _ height); trace ("_ top_left.width =", _ top_left.width ); // locate the picture on the top left _ top_left.x = _ top_left.y = 0; // locate the picture on the top right _ top_right.x = _ width-_ top_left.width; _ top_right.y = 0; // locate the bottom left image _ bottom_left.x = 0; _ bottom_left.y = _ height-_ bottom_left.height; // locate the bottom right image _ bottom_right.x = _ width-_ bottom_right.width; _ bottom_right.y = _ height-_ bottom_right.height; // The _ center image in the center. X = (_ width-_ center. width)/2; _ center. y = (_ height-_ center. height)/2 ;}}}
Two sheets:
Online Demo address: http://img.24city.com/jimmy/resize/resizedemo.html