AS3 CookBook学习整理(一)

来源:互联网 发布:新版淘宝联盟怎么用 编辑:程序博客网 时间:2024/05/30 05:17

1. 我要改变swf的尺寸和颜色

解决方法:
在flex builder 3里,默认会生成一个全屏、背景色为#869CA7、帧数为24/秒的swf文件,要修改这些参数,只需要在类文件中定义
[SWF(width="800", height="600", backgroundColor="#ffffff", frameRate="31")]

Example:

  1. package hxw
  2. {
  3.     import flash.display.Sprite;
  4.     [SWF(width="800", height="600", backgroundColor="#ff0000", frameRate="31")]
  5.     public class ExampleApplication extends Sprite
  6.     {
  7.         public function ExampleApplication()
  8.         {
  9.     
  10.         }
  11.     }
  12. }

2. 我要重复执行某段代码

解决方法:
在enterFrame事件中添加监听器和关联处理方法

Example:

  1. package hxw
  2. {
  3.     import flash.display.Sprite;
  4.     import flash.events.Event;
  5.     public class Sample1009 extends Sprite
  6.     {
  7.         public function Sample1009()
  8.         {
  9.             graphics.lineStyle(3,0xFF0000,1);
  10.             addEventListener(Event.ENTER_FRAME,onEnterFrame);
  11.         }
  12.         
  13.         private function onEnterFrame(event:Event):void
  14.         {
  15.             graphics.lineTo(Math.random()*400,Math.random()*400);
  16.         }
  17.     }
  18. }

3. 如何响应鼠标事件

解决方法:
为MouseEvent系列事件添加监听器和关联处理方法

Example:

  1. package hxw
  2. {
  3.     import flash.display.Sprite;
  4.     import flash.events.MouseEvent;
  5.     
  6.     [SWF(width="800", height="600", backgroundColor="#869CA7", frameRate="31")]
  7.     public class Sample1010 extends Sprite
  8.     {
  9.         private var _sprite:Sprite;
  10.         public function Sample1010()
  11.         {
  12.             _sprite = new Sprite();
  13.             _sprite.graphics.beginFill(0x27496E);
  14.             _sprite.graphics.drawRect(5,5,400,400);
  15.             _sprite.graphics.endFill();
  16.         
  17.             _sprite.addEventListener(MouseEvent.MOUSE_DOWN,OnMouseDown);
  18.             _sprite.addEventListener(MouseEvent.MOUSE_UP,OnMouseUp);
  19.             
  20.             this.addChild(_sprite);
  21.         }
  22.         
  23.         private function OnMouseDown(event:MouseEvent):void
  24.         {
  25.             _sprite.graphics.lineStyle(1,0xFFFF00,1);
  26.             _sprite.graphics.moveTo(mouseX,mouseY);
  27.             _sprite.addEventListener(MouseEvent.MOUSE_MOVE,OnMouseMove);
  28.         }
  29.         
  30.         private function OnMouseUp(event:MouseEvent):void
  31.         {
  32.             _sprite.removeEventListener(MouseEvent.MOUSE_MOVE,OnMouseMove);
  33.         }
  34.         
  35.         private function OnMouseMove(event:MouseEvent):void
  36.         {   
  37.             _sprite.graphics.lineTo(mouseX,mouseY);
  38.         }
  39.     }
  40. }

4. 如何响应键盘事件

解决方法:
为KeyboardEvent事件添加监听器和关联处理方法

Example:

  1. package {
  2.     import flash.display.Sprite;
  3.     import flash.events.KeyboardEvent;
  4.     public class Sample1030 extends Sprite
  5.     {
  6.         public function Sample1030()
  7.         {
  8.             //stage.focus = this;
  9.             stage.addEventListener(KeyboardEvent.KEY_DOWN,OnKeyDown);
  10.         }
  11.         
  12.         private function OnKeyDown(event:KeyboardEvent):void
  13.         {
  14.             trace(event.charCode);
  15.         }
  16.     }
  17. }

5. 如何实现定时器(Timer)

解决方法:
初始化一个Timer类,使用addEventListener来设置一个函数处理这个事件,然后使用timer的start( )方法启动或stop( )停止它。

Example:

  1. package {
  2.     import flash.display.Sprite;
  3.     import flash.events.TimerEvent;
  4.     import flash.utils.Timer;
  5.     public class Sample1101 extends Sprite
  6.     {
  7.         private var _rect:Sprite;
  8.         private var _circle:Sprite;
  9.         
  10.         public function Sample1101()
  11.         {
  12.             _rect = new Sprite();
  13.             _circle = new Sprite();
  14.             
  15.             _rect.graphics.beginFill(0xFFFF00);
  16.             _rect.graphics.drawRect(0,0,100,100);
  17.             _rect.graphics.endFill();
  18.             _rect.x = 50;
  19.             _rect.y = 100;
  20.             
  21.             this.addChild(_rect);
  22.             
  23.             _circle.graphics.beginFill(0x80C56E);
  24.             _circle.graphics.drawCircle(0,0,50);
  25.             _circle.graphics.endFill();
  26.             _circle.x = 100;
  27.             _circle.y = 200;
  28.             
  29.             this.addChild(_circle);
  30.             
  31.             var _timer:Timer = new Timer(50);
  32.             _timer.addEventListener(TimerEvent.TIMER,OnTimerTick);
  33.             _timer.addEventListener(TimerEvent.TIMER,OnTimerTick2);
  34.             _timer.start();
  35.         }
  36.         
  37.         private function OnTimerTick(event:TimerEvent):void
  38.         {
  39.             _rect.x += 20;
  40.         }
  41.         
  42.         private function OnTimerTick2(event:TimerEvent):void
  43.         {
  44.             _circle.y += 30;
  45.         }
  46.     }
  47. }

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

Example:

  1. package {
  2.     import flash.display.Sprite;
  3.     import flash.system.Capabilities;
  4.     public class Sample1101 extends Sprite
  5.     {
  6.         public function Sample1101()
  7.         {
  8.             var os:String = flash.system.Capabilities.os.substr(0,3);
  9.             if (os == "Win") {
  10.             // Windows-specific code goes here
  11.             }
  12.             else if (os == "Mac") {
  13.             // Mac-specific code goes here
  14.             }
  15.             else {
  16.             // Must be Unix or Linux
  17.             }
  18.         }
  19.     }
  20. }

7. 获得客户端的播放器类型

解决方法:
使用flash.system.Capabilities.playerType属性。它可能是PlugIn, ActiveX,StandAlone和External。

播放器的类型有:

浏览器插件形式存在于Mozilla 或Firefox

ActiveX 控件形式存在于Internet Explorer

独立播放器

外部播放器,它与Flash IDE进行交互

Example:

  1. package {
  2.     import flash.display.Sprite;
  3.     import flash.system.Capabilities;
  4.     public class Sample1101 extends Sprite
  5.     {
  6.         public function Sample1101()
  7.         {
  8.             if(flash.system.Capabilities.playerType == "Plugin")
  9.             {
  10.                 // do actions for Mozilla, etc. browsers
  11.             }
  12.             else if(flash.system.Capabilities.playerType == "ActiveX")
  13.             {
  14.                 // do actions for IE
  15.             }
  16.             else
  17.             {
  18.                 // do actions for no browser
  19.             }
  20.         }
  21.     }
  22. }

8. 获得客户端的语言与输入法

解决方法:
使用flash.system.Capabilities.language 属性和flash.system.IME 类

Example:

  1. package {
  2.     import flash.display.Sprite;
  3.     import flash.system.IME;
  4.     public class Sample1101 extends Sprite
  5.     {
  6.         public function Sample1101()
  7.         {
  8.             //从capabilities 对象上得到语言值
  9.             var lang:String = flash.system.Capabilities.language.substr(0, 2);
  10.             // 创建支持语言数组
  11.             var supportedLanguages:Array = ["en""es""fr"];
  12.             // 设置默认语言.
  13.             var useLang:String = "en";
  14.             //循环匹配,如果找到,设置useLang
  15.             for (var i:int = 0; i < supportedLanguages.length; i++)
  16.             {
  17.                 if (supportedLanguages[i] == lang)
  18.                 {
  19.                     useLang = lang;
  20.                     break;
  21.                 }
  22.             }
  23.             // 载入对应Flash
  24.             var movieURL:String = "myMovie_" + useLang + ".swf";
  25.         }
  26.     }
  27. }

9. 获得客户端的分辨率

解决方法:
screenResolutionX 和screenResolutionY 属性返回桌面的显示分辨率:

trace(flash.system.Capabilities.screenResolutionX);

trace(flash.system.Capabilities.screenResolutionY);

// 1024

// 768

Example:

  1. package {
  2.     import flash.display.Sprite;
  3.     import flash.external.ExternalInterface;
  4.     import flash.system.Capabilities;
  5.     public class Sample1101 extends Sprite
  6.     {
  7.         public function Sample1101()
  8.         {
  9.             var screenX:int = flash.system.Capabilities.screenResolutionX;
  10.             var screenY:int = flash.system.Capabilities.screenResolutionY;
  11.             
  12.             var winW:int = 200;
  13.             var winH:int = 200;
  14.             
  15.             var winX:int = (screenX / 2) - (winW / 2);
  16.             var winY:int = (screenY / 2) - (winH / 2);
  17.             
  18.             var jsCode:String = "javascript:void(newWin=window.open('http://www.person13.com/'," +
  19.             "'newWindow', 'width=" + winW +
  20.             ", height=" + winH + "," +
  21.             "left=" + winX + ",top=" + winY + "'));";
  22.             
  23.             ExternalInterface.call(jsCode);
  24.         }
  25.     }
  26. }

10. 缩放影片

解决方法:
设置stage.scaleMode,scaleMode属性值并不影响右键菜单里功能,不过你可以禁用菜单里的缩放功能。

Example:

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

这种模式不进行缩放,保持原有比例。使用该模式不要忘了设置对齐方式。

原创粉丝点击