1、本文介绍如何建立一个舞台随窗体尺寸大小动态改变的Flash例子,使用的脚本为AS3.0 1. 建立工程 2. 新建一个AS文件,主要实现舞台的动态缩放功能 代码如下: package { import flash.display.Sprite; import flash.display.Stage; import flash.events.Event; import flash.external.ExternalInterface; public class BrowserCanvas { public static const HA
2、CK_MARGIN_BOTTOM:String = "marginBottom"; //Adds a negative bottom margin to object tags to compensate for browser weirdness public static const HACK_IE_REPARENT:String = "IEReparent"; //In IE, create a container div which encapsulates the object tag in order to hav min/max sizes work public s
3、tatic const HACK_UNIQUE_ID:String = "uniqueId"; //If you put both an embed and object tag with the same id, this tries to compensate private var stage:Stage; private var containerId:String; private var _width:String; private var _minWidth:String; private var _maxWidth:Stri
4、ng; private var _height:String; private var _minHeight:String; private var _maxHeight:String; private var timerSprite:Sprite; public function BrowserCanvas ( stage:Stage,containerId:String=null , browserHacks:Array=null ) { trace("BrowserCanvas - Copyright (c) 2008 No
5、el Billig ()"); this.stage = stage; timerSprite = new Sprite(); _width = String( stage.stageWidth ); _height = String( stage.stageHeight ); if (browserHacks == null) browserHacks = [HACK_MARGIN_BOTTOM,HACK_IE_REPARENT,HACK_UNIQUE_ID]; this.containerId = contain
6、erId; if (this.containerId == null) this.containerId = ExternalInterface.objectID; if (this.containerId == null) this.containerId = ExternalInterface.call(JSScripts.GET_FLASH_ID, stage.loaderInfo.url); if (browserHacks.length != 0) { this.containerId = ExternalInterface.call(JS
7、Scripts.INSERT_BROWSER_HACKS, this.containerId, browserHacks.join(",")); } } public function set width(newWidth:String):void { this._width = formatSize(newWidth); invalidate(); } public function set minWidth(newWidth:String):void { this._minWidth
8、 formatSize(newWidth); invalidate(); } public function set maxWidth(newWidth:String):void { this._maxWidth = formatSize(newWidth); invalidate(); } public function set height(newHeight:String):void { this._height = formatSize(newHeight); invalidate();
9、 } public function set minHeight(newHeight:String):void { this._minHeight = formatSize(newHeight); invalidate(); } public function set maxHeight(newHeight:String):void { this._maxHeight = formatSize(newHeight); invalidate(); } private function formatSize(
10、size:String):String { if (size == null) return ""; //Null causes opera to never clear the appropriate values, so use empty string return (int(size) == 0) ? size : size+"px"; } private function invalidate():void { timerSprite.addEventListener(Event.ENTER_FRAME,update); }
11、 private function update(event:Event):void { timerSprite.removeEventListener(Event.ENTER_FRAME,update); ExternalInterface.call(JSScripts.RESIZE_CONTAINER,containerId,_width,_height,_minWidth,_minHeight,_maxWidth,_maxHeight); } } } class JSScripts { public static va
12、r GET_FLASH_ID:XML = ; public static var INSERT_BROWSER_HACKS:XML = ; public static var RESIZE_CONTAINER:XML = ; } 3. 建立Flash文件场景相关的类 package { import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; import flash.events
31、MouseEvent; import flash.text.TextField; import flash.text.TextFormat; //导入第2节中建立的类 import BrowserCanvas; [SWF(width="550", height="400", frameRate="30", backgroundColor="#000000")] public class ResizeSceneClass extends Sprite { private var canvas:BrowserCanvas; publ
32、ic function ResizeSceneClass () { //Set up the stage so we don't scale (you may not want to do this, depending on your desired goal) stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; canvas = new BrowserCanvas(stage); stage.addEventListener(MouseEvent.CLICK,randomSize); } private function randomSize(event:Event=null):void { canvas.width = String( Math.random()*500 + 200); canvas.height = String( Math.random()*500 + 200); } } } 4. 把ResizeSceneClass和第1节建立的Flash文件相关 到此完成






