as3學習筆記7:as3全屏功能
阿新 • • 發佈:2018-12-31
開發環境:FlashDevelop
步驟:
1.新建一個as3工程:FullScreen
目錄:E:/ActionScript/FullScreen
2.在Main.as中敲入如下程式碼:
說明:as3的全屏功能的實現主要是舞臺stage的displayState屬性package { import flash.display.Bitmap; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageDisplayState; import flash.display.StageScaleMode; import flash.events.KeyboardEvent; import flash.events.MouseEvent; public class Main extends Sprite { [Embed(source = "../image/bg.jpg")] private var BG:Class; private var mBg:Bitmap; [Embed(source = "../image/btn_0.png")] private var BTN0:Class; private var mBtn0:Bitmap; [Embed(source = "../image/btn_1.png")] private var BTN1:Class; private var mBtn1:Bitmap; [Embed(source = "../image/btn_2.png")] private var BTN2:Class; private var mBtn2:Bitmap; public function Main():void { //設定舞臺 stage.displayState = StageDisplayState.NORMAL; stage.scaleMode = StageScaleMode.SHOW_ALL; stage.align = StageAlign.TOP; //新增舞臺按鍵監聽(按ESC鍵取消最大化) stage.addEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown); mBg = new BG as Bitmap; mBtn0 = new BTN0 as Bitmap; mBtn1 = new BTN1 as Bitmap; mBtn2 = new BTN2 as Bitmap; //新增背景 addChild(mBg); //新增按鈕 var button:MyButton = new MyButton; button.SetParameter(mBtn0.bitmapData, mBtn1.bitmapData, mBtn2.bitmapData); addChild(button); button.x = 100; button.y = 100; button.addEventListener(MouseEvent.CLICK, OnFullScreemClick); } //ESC鍵取消全屏 private function OnKeyDown(e:KeyboardEvent):void { switch(e.keyCode) { case 27: //ESC鍵值 stage.displayState = StageDisplayState.NORMAL; break; } } //全屏按鈕事件 private function OnFullScreemClick(e:MouseEvent):void { switch(stage.displayState) { case StageDisplayState.NORMAL: stage.displayState = StageDisplayState.FULL_SCREEN; break; case StageDisplayState.FULL_SCREEN_INTERACTIVE: //flash開啟的全屏引數 case StageDisplayState.FULL_SCREEN: //網頁開啟的全屏引數 stage.displayState = StageDisplayState.NORMAL; break; } } } }
StageDisplayState.NORMAL 正常
StageDisplayState.FULL_SCREEN_INTERACTIVE flash開啟swf時全屏的狀態
StageDisplayState.FULL_SCREEN 網頁中的swf全屏的狀態
然後參考上面按鍵響應函式內的程式碼和滑鼠響應函式內的程式碼即可實現全屏。