AS3 CookBook学习整理(一)
1. 我要改变swf的尺寸和颜色
在flex builder 3里,默认会生成一个全屏、背景色为#869CA7、帧数为24/秒的swf文件,要修改这些参数,只需要在类文件中定义 [SWF(width="800", height="600", backgroundColor="#ffffff", frameRate="31")]
package hxw { import flash.display.Sprite; [SWF(width="800", height="600", backgroundColor="#ff0000", frameRate="31")] public class ExampleApplication extends Sprite { public function ExampleApplication() { } } }
2. 我要重复执行某段代码
在enterFrame事件中添加监听器和关联处理方法
package hxw { import flash.display.Sprite; import flash.events.Event; public class Sample1009 extends Sprite { public function Sample1009() { graphics.lineStyle(3,0xFF0000,1); addEventListener(Event.ENTER_FRAME,onEnterFrame); } private function onEnterFrame(event:Event):void { graphics.lineTo(Math.random()*400,Math.random()*400); } } }
3. 如何响应鼠标事件
为MouseEvent系列事件添加监听器和关联处理方法
package hxw { import flash.display.Sprite; import flash.events.MouseEvent; [SWF(width="800", height="600", backgroundColor="#869CA7", frameRate="31")] public class Sample1010 extends Sprite { private var _sprite:Sprite; public function Sample1010() { _sprite = new 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); this.addChild(_sprite); } private function OnMouseDown(event:MouseEvent):void { _sprite.graphics.lineStyle(1,0xFFFF00,1); _sprite.graphics.moveTo(mouseX,mouseY); _sprite.addEventListener(MouseEvent.MOUSE_MOVE,OnMouseMove); } private function OnMouseUp(event:MouseEvent):void { _sprite.removeEventListener(MouseEvent.MOUSE_MOVE,OnMouseMove); } private function OnMouseMove(event:MouseEvent):void { _sprite.graphics.lineTo(mouseX,mouseY); } } }
4. 如何响应键盘事件
为KeyboardEvent事件添加监听器和关联处理方法
package { import flash.display.Sprite; import flash.events.KeyboardEvent; public class Sample1030 extends Sprite { public function Sample1030() { //stage.focus = this; stage.addEventListener(KeyboardEvent.KEY_DOWN,OnKeyDown); } private function OnKeyDown(event:KeyboardEvent):void { trace(event.charCode); } } }
5. 如何实现定时器(Timer)
初始化一个Timer类,使用addEventListener来设置一个函数处理这个事件,然后使用timer的start( )方法启动或stop( )停止它。
package { import flash.display.Sprite; import flash.events.TimerEvent; import flash.utils.Timer; public class Sample1101 extends Sprite { private var _rect:Sprite; private var _circle:Sprite; public function Sample1101() { _rect = new Sprite(); _circle = new Sprite(); _rect.graphics.beginFill(0xFFFF00); _rect.graphics.drawRect(0,0,100,100); _rect.graphics.endFill(); _rect.x = 50; _rect.y = 100; this.addChild(_rect); _circle.graphics.beginFill(0x80C56E); _circle.graphics.drawCircle(0,0,50); _circle.graphics.endFill(); _circle.x = 100; _circle.y = 200; this.addChild(_circle); var _timer:Timer = new Timer(50); _timer.addEventListener(TimerEvent.TIMER,OnTimerTick); _timer.addEventListener(TimerEvent.TIMER,OnTimerTick2); _timer.start(); } private function OnTimerTick(event:TimerEvent):void { _rect.x += 20; } private function OnTimerTick2(event:TimerEvent):void { _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
package { import flash.display.Sprite; import flash.system.Capabilities; public class Sample1101 extends Sprite { public function Sample1101() { var os:String = flash.system.Capabilities.os.substr(0,3); if (os == "Win") { // Windows-specific code goes here } else if (os == "Mac") { // Mac-specific code goes here } else { // Must be Unix or Linux } } } }
7. 获得客户端的播放器类型
使用flash.system.Capabilities.playerType属性。它可能是PlugIn, ActiveX,StandAlone和External。
播放器的类型有:
浏览器插件形式存在于Mozilla 或Firefox
ActiveX 控件形式存在于Internet Explorer
独立播放器
外部播放器,它与Flash IDE进行交互
package { import flash.display.Sprite; import flash.system.Capabilities; public class Sample1101 extends Sprite { public function Sample1101() { if(flash.system.Capabilities.playerType == "Plugin") { // do actions for Mozilla, etc. browsers } else if(flash.system.Capabilities.playerType == "ActiveX") { // do actions for IE } else { // do actions for no browser } } } }
8. 获得客户端的语言与输入法
使用flash.system.Capabilities.language 属性和flash.system.IME 类
package { import flash.display.Sprite; import flash.system.IME; public class Sample1101 extends Sprite { public function Sample1101() { // 从capabilities 对象上得到语言值 var lang:String = flash.system.Capabilities.language.substr(0, 2); // 创建支持语言数组> var supportedLanguages:Array = ["en", "es", "fr"]; // 设置默认语言. var useLang:String = "en"; //循环匹配,如果找到,设置useLang for (var i:int = 0; i < supportedLanguages.length; i++) { if (supportedLanguages[i] == lang) { useLang = lang; break; } } // 载入对应Flash var movieURL:String = "myMovie_" + useLang + ".swf"; } } }
9. 获得客户端的分辨率
screenResolutionX 和screenResolutionY 属性返回桌面的显示分辨率:
trace(flash.system.Capabilities.screenResolutionX); // 1024
trace(flash.system.Capabilities.screenResolutionY); // 768
package { import flash.display.Sprite; import flash.external.ExternalInterface; import flash.system.Capabilities; public class Sample1101 extends Sprite { public function Sample1101() { var screenX:int = flash.system.Capabilities.screenResolutionX; var screenY:int = flash.system.Capabilities.screenResolutionY; var winW:int = 200; var winH:int = 200; var winX:int = (screenX / 2) - (winW / 2); var winY:int = (screenY / 2) - (winH / 2); var jsCode:String = "javascript:void(newWin=window.open('http://www.person13.com/'," + "'newWindow', 'width=" + winW + ", height=" + winH + "," + "left=" + winX + ",top=" + 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
这种模式不进行缩放,保持原有比例。使用该模式不要忘了设置对齐方式。