Flash Player 10.2添加了内置的本机鼠标指针(native mouse cursor)支持,虽然在之前的版本里我们可以侦听MouseEvent事件来模拟鼠标指针,但是在有了原生的本机鼠标指针后,我们可以舍弃旧的方式,迎接更友好,更方便的鼠标指针了。
下面对比下模拟鼠标指针和本机鼠标指针。
模拟鼠标指针:
- 鼠标指针不能拖到舞台外部。
- 使用MouseEvent事件,效率低下。
- 使用方式麻烦。
本机鼠标指针:
- 鼠标指针可拖到舞台外部。
- 原生支持效率高。
- 直接用Mouse类,很方便。
新的本机鼠标的一些能力及限制:
- 必须使用BitmapData为鼠标指针的可视对象。
- 鼠标指针尺寸不能超过32*32。
- 可以使用多个BitamapData来播放动画。
- 可以自定义动画播放的帧频,与当前SWF的帧频无关。
- 可以鼠标自定义鼠标指针的热点,而不总是在(0,0)位置。
- 要在舞台外显示本机鼠标指针,必须按下鼠标左键拖到舞台外面才行。
示例代码:
package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Sprite; import flash.events.Event; import flash.geom.Point; import flash.text.TextField; import flash.ui.Mouse; import flash.ui.MouseCursorData; /** * ... * @author Frost.Yen * @e-mail 871979853@qq.com * @create 2016/1/29 星期五 17:01 */ [SWF(backgroundColor = "0x000000", width = 800, height = 600)] public class Main extends Sprite { [Embed(source = "move.png")] private var Corsor:Class; public function Main() { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); var mouseCursorData:MouseCursorData = new MouseCursorData(); mouseCursorData.data = Vector.<BitmapData>([Bitmap(new Corsor()).bitmapData]); mouseCursorData.hotSpot = new Point(27, 5); Mouse.registerCursor("nativeMouseCursor", mouseCursorData); Mouse.cursor = "nativeMouseCursor"; var txt:TextField = new TextField(); txt.autoSize = "left"; txt.text = "按住鼠标并拖到Flash Player外部试试!"; txt.mouseEnabled = false; txt.opaqueBackground = 0xFFFFFFF; txt.x = (800-txt.width)*0.5; txt.y = 10; addChild(txt); } } }
注:运用[Embed(source = "move.png")]方式,必须使用Flex SDK 。
官方帮助:http://help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/flash/ui/Mouse.html