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
这种模式不进行缩放,保持原有比例。使用该模式不要忘了设置对齐方式。