tags:

views:

53

answers:

3

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.

+1  A: 
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);
Maxim Kachurovskiy
This worked, thanks!
justkevin
A: 

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

Vladimir Grigorov
This is a Flex project, so I can't addChild to the main mxml class.
justkevin
@justkevin: yes, you're right - in this case you'll have to place it in a UIComponent.
Vladimir Grigorov