AS3 外部读取图片

使用AS3 通过读取文件夹中的图片,达到给影片添加图片

package  
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.display.Bitmap;
	public class Image extends Sprite {
		public var imageNumber: int = 16;
		public var imgLoader: Loader = new Loader();
		public var imgURL: URLRequest = new URLRequest();
		
		public function Image() {
			ShowImage();
		} 
		public function ShowImage() {
			var num: int = this.stage.numChildren;
			while (num > 0) {
				removeChildAt(0);
				num--;
			}
			var randomNum: Number = Math.round((Math.random() * 10000)) % imageNumber + 1;
			imgURL.url = "resources/Gift" + randomNum + ".png";

			imgLoader.load(imgURL);
			imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, finished);

		}
		public function finished(evt: Event): void {
			imgLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, finished); 
			 
			var img: Bitmap = new Bitmap(evt.target.content.bitmapData);
			this.addChild(img); 
			img.x = 0; 
			img.y = 0;
			img.width = 200;
			img.height = 200;
		} 
	} 
}




你可能感兴趣的:(AS3)