AS3 CookBook学习整理(一)
1. 我要改變swf的尺寸和顏色
在flex builder 3里,默認會生成一個全屏、背景色為#869CA7、幀數為24/秒的swf文件,要修改這些參數,只需要在類文件中定義 [SWF(width="800", height="600", backgroundColor="#ffffff", frameRate="31")]
hxw {flash.display.Sprite;[SWF(width=, height=, backgroundColor=, frameRate=)]ExampleApplicationSprite{ExampleApplication(){}} }2. 我要重復執行某段代碼
在enterFrame事件中添加監聽器和關聯處理方法
hxw {flash.display.Sprite;flash.events.Event;Sample1009Sprite{Sample1009(){graphics.lineStyle(3,0xFF0000,1);addEventListener(Event.ENTER_FRAME,onEnterFrame);}onEnterFrame(event:Event):{graphics.lineTo(Math.random()*400,Math.random()*400);}} }3. 如何響應鼠標事件
為MouseEvent系列事件添加監聽器和關聯處理方法
hxw {flash.display.Sprite;flash.events.MouseEvent;[SWF(width=, height=, backgroundColor=, frameRate=)]Sample1010Sprite{_sprite:Sprite;Sample1010(){_sprite = Sprite();_sprite.graphics.beginFill(0x27496E);_sprite.graphics.drawRect(5,5,400,400);_sprite.graphics.endFill();_sprite.addEventListener(MouseEvent.MOUSE_DOWN,OnMouseDown);_sprite.addEventListener(MouseEvent.MOUSE_UP,OnMouseUp);.addChild(_sprite);}OnMouseDown(event:MouseEvent):{_sprite.graphics.lineStyle(1,0xFFFF00,1);_sprite.graphics.moveTo(mouseX,mouseY);_sprite.addEventListener(MouseEvent.MOUSE_MOVE,OnMouseMove);}OnMouseUp(event:MouseEvent):{_sprite.removeEventListener(MouseEvent.MOUSE_MOVE,OnMouseMove);}OnMouseMove(event:MouseEvent):{ _sprite.graphics.lineTo(mouseX,mouseY);}} }4. 如何響應鍵盤事件
為KeyboardEvent事件添加監聽器和關聯處理方法
{flash.display.Sprite;flash.events.KeyboardEvent;Sample1030Sprite{Sample1030(){stage.addEventListener(KeyboardEvent.KEY_DOWN,OnKeyDown);}OnKeyDown(event:KeyboardEvent):{(event.charCode);}} }5. 如何實現定時器(Timer)
初始化一個Timer類,使用addEventListener來設置一個函數處理這個事件,然后使用timer的start( )方法啟動或stop( )停止它。
{flash.display.Sprite;flash.events.TimerEvent;flash.utils.Timer;Sample1101Sprite{_rect:Sprite;_circle:Sprite;Sample1101(){_rect = Sprite();_circle = Sprite();_rect.graphics.beginFill(0xFFFF00);_rect.graphics.drawRect(0,0,100,100);_rect.graphics.endFill();_rect.x = 50;_rect.y = 100;.addChild(_rect);_circle.graphics.beginFill(0x80C56E);_circle.graphics.drawCircle(0,0,50);_circle.graphics.endFill();_circle.x = 100;_circle.y = 200;.addChild(_circle);_timer:Timer = Timer(50);_timer.addEventListener(TimerEvent.TIMER,OnTimerTick);_timer.addEventListener(TimerEvent.TIMER,OnTimerTick2);_timer.start();}OnTimerTick(event:TimerEvent):{_rect.x += 20;}OnTimerTick2(event:TimerEvent):{_circle.y += 30;}} }6. 獲得客戶端的操作系統版本
ActionScript 3.0中,flash.system.Capabilities.os 屬性返回操作系統名稱和版本字符串。值可能包括Windows XP, Windows 2000, Windows NT, Windows 98/Me, Windows 95, 和Windows CE. 在蘋果機上,字符串包括版本號,比如Mac OS 9.2.1 或Mac OS X 10.4.4
{flash.display.Sprite;flash.system.Capabilities;Sample1101Sprite{Sample1101(){os:String = flash.system.Capabilities.os.substr(0,3);if (os == ) {}else if (os == ) {}else {}}} }7. 獲得客戶端的播放器類型
使用flash.system.Capabilities.playerType屬性。它可能是PlugIn, ActiveX,StandAlone和External。
播放器的類型有:
瀏覽器插件形式存在于Mozilla 或Firefox
ActiveX 控件形式存在于Internet Explorer
獨立播放器
外部播放器,它與Flash IDE進行交互
{flash.display.Sprite;flash.system.Capabilities;Sample1101Sprite{Sample1101(){(flash.system.Capabilities.playerType == ){}else (flash.system.Capabilities.playerType == ){}else{}}} }8. 獲得客戶端的語言與輸入法
使用flash.system.Capabilities.language 屬性和flash.system.IME 類
{flash.display.Sprite;flash.system.IME;Sample1101Sprite{Sample1101(){lang:String = flash.system.Capabilities.language.substr(0, 2);>?supportedLanguages:Array = [, , ];useLang:String = ;for (i:int = 0; i < supportedLanguages.length; i++){if (supportedLanguages[i] == lang){useLang = lang;break;}}movieURL:String = + useLang + ;}} }9. 獲得客戶端的分辨率
screenResolutionX 和screenResolutionY 屬性返回桌面的顯示分辨率:
trace(flash.system.Capabilities.screenResolutionX); // 1024
trace(flash.system.Capabilities.screenResolutionY); // 768
{flash.display.Sprite;flash.external.ExternalInterface;flash.system.Capabilities;Sample1101Sprite{Sample1101(){screenX:int = flash.system.Capabilities.screenResolutionX;screenY:int = flash.system.Capabilities.screenResolutionY;winW:int = 200;winH:int = 200;winX:int = (screenX / 2) - (winW / 2);winY:int = (screenY / 2) - (winH / 2);jsCode:String = ++ winW ++ winH + ++ winX + + winY + ;ExternalInterface.call(jsCode);}} }10. 縮放影片
設置stage.scaleMode,scaleMode屬性值并不影響右鍵菜單里功能,不過你可以禁用菜單里的縮放功能。
stage.scaleMode的值來自flash.display.StageScaleMode類的枚舉,有EXACT_FIT, NO_BORDER,NO_SCALE, 和SHOW_ALL
假設原影片如下:
1. SHOW_ALL
這種模式會成比例縮小與放大。如果播放器與影片的比例不一致,則會出現空白邊框。以SHOW_ALL模式縮小后的效果如下:
2. EXACT_FIT
這種模式會不成比例縮小與放大。以EXACT_FIT模式縮小后的效果如下:
3. NO_BORDER
這種模式會成比例縮小與放大。如果播放器和影片比例不一致,則會裁剪影片。以NO_BORDER模式縮小后的效果如下:
4. NO_SCALE
這種模式不進行縮放,保持原有比例。使用該模式不要忘了設置對齊方式。
轉載于:https://www.cnblogs.com/CoderWayne/archive/2010/07/15/1778031.html
總結
以上是生活随笔為你收集整理的AS3 CookBook学习整理(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 记录ubuntu18.04安装QQ过程
- 下一篇: LitePal查询