开发者

Change mouse cursor to a bitmap (Flex 4)?

开发者 https://www.devze.com 2023-01-04 10:42 出处:网络
In Flex 4, how can I change the cursor to a Bitmap image determined at runtime? All the examples I\'ve seen use CursorManager.setCursor to s开发者_JS百科et the cursor to a class specified at compile t

In Flex 4, how can I change the cursor to a Bitmap image determined at runtime? All the examples I've seen use CursorManager.setCursor to s开发者_JS百科et the cursor to a class specified at compile time.

What I want to do is change the cursor to a bitmap whose bitmapData is determined by the context.


package cursor
{
import flash.display.BitmapData;
import flash.display.PixelSnapping;

import mx.core.BitmapAsset;

public class RuntimeBitmap1 extends BitmapAsset
{

    public static var staticBitmapData:BitmapData;

    public function RuntimeBitmap1()
    {
        super(staticBitmapData);
    }
}
}

Usage:

var bitmapData:BitmapData = new BitmapData(50, 50, false, 0x88888888);
RuntimeBitmap1.staticBitmapData = bitmapData;
cursorManager.setCursor(RuntimeBitmap1, 0);


I wanted to draw a UIComponent as a cursor.

I managed it using a combination of Maxims answer and this Flex Cookbox article. The only change I had to make to Maxim answer was a s follows:

public function RuntimeBitmap1()
{
    super(RuntimeBitmap1.staticBitmapData);
}

Otherwise staticBitmapData came through as null in the constructor.


Here are a few simple steps to change the default cursor with a bitmap image:

  1. Create your cursor of type Bitmap by using an image of your choice. You can also set the bitmapData dynamically during runtime.
    
    var DEFAULT_CURSOR_IMAGE : Class;
    var myCursorBitmap : Bitmap;
    ...
    myCursorBitmap = new DEFAULT_CURSOR_IMAGE();
  2. Register to receive mouse move events and update cursor position accordingly.
    
    function onMouseMove(event : MouseEvent) : void
    {
       myCursorBitmap.x = event.localX;
       myCursorBitmap.y = event.localY;
    }
  3. Hide the real cursor by using Mouse.hide().

  4. Show your custom cursor. You may update cursor shape later by setting bitmapData dynamically.

    
    addChild(myCursorBitmap);
    ...
    myCursorBitmap.bitmapData = myNewCursor;

To restore the default cursor, remove your cursor bitmap from the stage and call Mouse.show().

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号