Adobe Flash Player 10.2 版本引入了一个引人注目的新特性:原生鼠标指针。您现在可以使用运行在操作系统层的基于位图的鼠标指针。
实现原生鼠标指针
flash.ui 包中的 MouseCursorData 对象
MouseCursorData 对象的三个属性:
- MouseCursorData.data:用于显示鼠标指针的 BitmapData 对象向量。
- MouseCursorData.hotSpot:鼠标指针的定位点值,保存为一个 Point 对象。
- MouseCursorData.frameRate:用于实现位图图像序列动画的帧频。这个属性允许您创建动画鼠标指针。
在创建一个 MouseCursorData 对象之后,您要使用 Mouse.registerCursor 方法将它赋值给 Mouse 对象。一旦注册了 Mouse 对象,您可以将别名传递给 Mouse.cursor 属性。
说明:通过传递一个 BitmapData ,您就可以通过指定一系列的位图鼠标指针来创建一个原生的动画指针。
请查看以下示例代码:
// Create a MouseCursorData object
var cursorData:MouseCursorData = new MouseCursorData();
// Specify the hotspot
cursorData.hotSpot = new Point(15,15);
// Pass the cursor bitmap to a BitmapData Vector
var bitmapDatas:Vector.<BitmapData> = new Vector.<BitmapData>(1, true);
// Create the bitmap cursor
// The bitmap must be 32x32 pixels or smaller, due to an OS limitation
var bitmap:Bitmap = new zoomCursor();
// Pass the value to the bitmapDatas vector
bitmapDatas[0] = bitmap.bitmapData;
// Assign the bitmap to the MouseCursor object
cursorData.data = bitmapDatas;
// Register the MouseCursorData to the Mouse object with an alias
Mouse.registerCursor("myCursor", cursorData);
// When needed for display, pass the alias to the existing cursor property
Mouse.cursor = "myCursor";
var cursorData:MouseCursorData = new MouseCursorData();
// Specify the hotspot
cursorData.hotSpot = new Point(15,15);
// Pass the cursor bitmap to a BitmapData Vector
var bitmapDatas:Vector.<BitmapData> = new Vector.<BitmapData>(1, true);
// Create the bitmap cursor
// The bitmap must be 32x32 pixels or smaller, due to an OS limitation
var bitmap:Bitmap = new zoomCursor();
// Pass the value to the bitmapDatas vector
bitmapDatas[0] = bitmap.bitmapData;
// Assign the bitmap to the MouseCursor object
cursorData.data = bitmapDatas;
// Register the MouseCursorData to the Mouse object with an alias
Mouse.registerCursor("myCursor", cursorData);
// When needed for display, pass the alias to the existing cursor property
Mouse.cursor = "myCursor";
一定要记住,由于操作系统的限制,鼠标指针所使用的这些位图文件不能大于 32 × 32 像素。传递一个大于此限制的位图会出错。
无论任何时候,您都可以停止使用当前的位图鼠标指针,而切换回显示默认操作系统鼠标指针。要实现这一点,您可以使用 MouseCursor 类的其中一个常量值,如下所示:
Mouse.cursor = MouseCursor.AUTO;
上一个例子创建了一个静态位图鼠标指针;下一个例子将创建一个动画鼠标指针。这个过程是很简单的 —— 只需要先提供多个位图图像,然后指定鼠标指针动画的帧频,如下所示:
// Create a MouseCursorData object
var cursorData:MouseCursorData = new MouseCursorData();
// Specify the hotspot
cursorData.hotSpot = new Point(15,15);
// Pass the cursor's bitmap to a BitmapData Vector
var bitmapDatas:Vector.<BitmapData> = new Vector.<BitmapData>(3, true);
// Create the bitmap cursor frames
// Bitmaps must be 32 x 32 pixels or less, due to an OS limitation
var frame1Bitmap:Bitmap = new frame1();
var frame2Bitmap:Bitmap = new frame2();
var frame3Bitmap:Bitmap = new frame3();
// Pass the values of the bitmap files to the bitmapDatas vector
bitmapDatas[0] = frame1Bitmap.bitmapData;
bitmapDatas[1] = frame2Bitmap.bitmapData;
bitmapDatas[2] = frame3Bitmap.bitmapData;
// Assign the bitmap data to the MouseCursor object
cursorData.data = bitmapDatas;
// Pass the frame rate of the animated cursor (1fps)
cursorData.frameRate = 1;
// Register the MouseCursorData to the Mouse object
Mouse.registerCursor("myAnimatedCursor", cursorData);
// When needed for display, pass the alias to the existing cursor property
Mouse.cursor = "myAnimatedCursor";
var cursorData:MouseCursorData = new MouseCursorData();
// Specify the hotspot
cursorData.hotSpot = new Point(15,15);
// Pass the cursor's bitmap to a BitmapData Vector
var bitmapDatas:Vector.<BitmapData> = new Vector.<BitmapData>(3, true);
// Create the bitmap cursor frames
// Bitmaps must be 32 x 32 pixels or less, due to an OS limitation
var frame1Bitmap:Bitmap = new frame1();
var frame2Bitmap:Bitmap = new frame2();
var frame3Bitmap:Bitmap = new frame3();
// Pass the values of the bitmap files to the bitmapDatas vector
bitmapDatas[0] = frame1Bitmap.bitmapData;
bitmapDatas[1] = frame2Bitmap.bitmapData;
bitmapDatas[2] = frame3Bitmap.bitmapData;
// Assign the bitmap data to the MouseCursor object
cursorData.data = bitmapDatas;
// Pass the frame rate of the animated cursor (1fps)
cursorData.frameRate = 1;
// Register the MouseCursorData to the Mouse object
Mouse.registerCursor("myAnimatedCursor", cursorData);
// When needed for display, pass the alias to the existing cursor property
Mouse.cursor = "myAnimatedCursor";
通过设置 MouseCursorData.frameRate 属性并传入一系列 BitmapData 对象,Flash Player 就会自动创建出一个以指定帧频播放的动画鼠标指针。这是一个自动化的过程,所以您不需要编写任何代码就能够实现动画鼠标指针。