自定义光标一直是用Mouse.hide()然后自己做个图形,鼠标跟随。其实flash从10.2开始就支持注册系统光标了。
鼠标跟随实现的光标总是感觉有些延时,注册系统光标效果好很多。不知道为什么很少有人用。
demo:
实现代码如下,学习的话,可以去看官方API的例子,写的很好。
package { import flash.display.BitmapData; import flash.display.MovieClip; import flash.display.SimpleButton; import flash.events.MouseEvent; import flash.geom.Point; import flash.text.TextField; import flash.ui.Mouse; import flash.ui.MouseCursor; import flash.ui.MouseCursorData; public class Main extends MovieClip { public var btn:MovieClip; public var tf:TextField; private var text:String; private var cursorNames:Array = ["cross", "rotateCross","auto"]; private var n:int = 0; public function Main() { // constructor code trace(Mouse.supportsNativeCursor); if (Mouse.supportsNativeCursor) { tf.text = "支持系统光标"; } else { tf.text = "不支持系统光标"; } btn.tf.text = text = cursorNames[n]; btn.gotoAndStop(1); btn.buttonMode = true; btn.mouseChildren = false; btn.addEventListener(MouseEvent.CLICK, btnClickHandler); test01(); test02(); Mouse.cursor = text; } private function btnClickHandler(e:MouseEvent):void { n++; if (n>=cursorNames.length) { n -= cursorNames.length; } btn.tf.text = text = cursorNames[n]; Mouse.cursor = text; } private function test02():void { var cursorData:MouseCursorData = new MouseCursorData(); cursorData.data = new Vector.<BitmapData>(); var cursorMc:MovieClip = new CursorMc(); for (var i:int = 1, len:int = cursorMc.totalFrames; i <= len; i++ ) { var bmd:BitmapData = new BitmapData(30,30,true,0x00000000); cursorMc.gotoAndStop(i); bmd.draw(cursorMc); cursorData.data.push(bmd); } cursorData.hotSpot = new Point(15, 15); cursorData.frameRate = 16; Mouse.registerCursor("rotateCross", cursorData); } private function test01():void { var cursorData:MouseCursorData = new MouseCursorData(); var bmd:BitmapData = new CrossBmd(); cursorData.data = new Vector.<BitmapData>(); cursorData.data.push(bmd); cursorData.hotSpot = new Point(16, 16); Mouse.registerCursor("cross", cursorData); } } }Mouse.supportsNativeCursor检测系统是否支持注册光标,官方说“在大多数桌面计算机上 supportsCursor 属性为 true,而在大多数移动设备上为 false”。
Mouse.registerCursor(name:String, cursor:MouseCursorData):void 注册光标。
需要new一个MouseCursorData,指定data属性,data属性是一个由BitmapData组成的向量,hotSpot属性指定光标的注册点位置,如果data里边有多个BitmapData的话,还可以指定frameRate,实现动态的光标。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。