Cameron
Cameron

Reputation: 28853

Native cursor in Adobe AIR JavaScript with MouseCursorData

I've been reading this article: http://blogs.adobe.com/cantrell/archives/2011/03/native-cursors-in-air-2-6.html on how to create a native cursor in AIR without having to hack it by moving a sprite in place of a hidden cursor to fake it.

However I'm using HTML/JavaScript instead of ActionScript.

So far I have:

function nativeCursor(){

    // load in a bitmap
    var loader = new air.Loader();
    loader.load(new air.URLRequest('./assets/cursor.png'));

    var bitmaps = new air.Vector["<String>"]();

    var bmd = new air.BitmapData(32, 32, true, 0x00000000);

    var p = new window.runtime.flash.geom.Point(0, 0);
    var r = new window.runtime.flash.geom.Rectangle(32 , 0, 32, 32);

    var image = new window.runtime.flash.display.Bitmap(loader.content);

    bmd.copyPixels([image.bitmapData], r, p);

    bitmaps.push(bmd);

    var mcd = new window.runtime.flash.ui.MouseCursorData();

    mcd.data = bitmaps;
    mcd.hotSpot = new Point(0, 0);
    mcd.frameRate = 24;

    window.runtime.flash.ui.Mouse.registerCursor("defaultCursor", mcd);
    window.runtime.flash.ui.Mouse.cursor = "defaultCursor";

}

But I get an error TypeError: Error #1034: Type Coercion failed: cannot convert []@2b9d1f1 to flash.display.BitmapData. for this line: bmd.copyPixels([image.bitmapData], r, p);

If I remove the brackets for that line so it's just: bmd.copyPixels(image.bitmapData, r, p); the error becomes TypeError: Error #2007: Parameter sourceBitmapData must be non-null.

So I'm assuming that the error is because the bitmap data is null... but why? The image is being loaded in fine so is the way I'm trying to get the bitmap data incorrect?

Upvotes: 0

Views: 249

Answers (1)

ndm
ndm

Reputation: 60493

BitmapData, not String

The vector is supposed to be of type BitmapData, not String, that is:

air.Vector["<flash.display.BitmapData>"]

See HTML Developer’s Guide for Adobe AIR - Working with Vectors for more information.

Asynchronous loaders

Also the Loader class probably works asynchronously in JavaScript too, it's not documented properly in the HTML API reference and I've never used JS for AIR development, so I can only assume that, and that one can refer to the AS3 reference for the missing docs, however it makes sense judging from the available examples.

http://help.adobe.com/.../html/flash/display/BitmapData.html#includeExamplesSummary

Loader.bitmapData doesn't exist

There is no bitmapData property on the Loader class, only a content property that holds a DisplayObject, which might actually be a Bitmap object which in turn has a bitmapData property.

An example

Here's some untested example code that should get you started:

var mcd = new window.runtime.flash.ui.MouseCursorData();
mcd.hotSpot = new air.Point(0, 0);
mcd.frameRate = 24;

var loader = new air.Loader();
loader.contentLoaderInfo.addEventListener(air.Event.COMPLETE, function(event)
{       
    var image = air.Bitmap(loader.content);     

    var bitmaps = new air.Vector["<flash.display.BitmapData>"]();
    bitmaps.push(image.bitmapData);

    mcd.data = bitmaps;

    air.Mouse.registerCursor('defaultCursor', mcd);
    air.Mouse.cursor = 'defaultCursor';
});

var request = new air.URLRequest('./assets/cursor.png');
loader.load(request);

Upvotes: 1

Related Questions