justkevin
justkevin

Reputation: 3199

Change mouse cursor to a bitmap (Flex 4)?

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 set 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.

Upvotes: 0

Views: 1887

Answers (3)

user493612
user493612

Reputation: 11

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.

Upvotes: 1

Vlad Grigorov
Vlad Grigorov

Reputation: 11376

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().

Upvotes: 0

Maxim Kachurovskiy
Maxim Kachurovskiy

Reputation: 3022

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);

Upvotes: 2

Related Questions