Bisher wurden eigene Mauscursor in Flash und AIR so realisiert, dass der eigentliche Mauscursor ausgeblendet und stattdessen ein Displayobject anhand der Mausposition animiert wurde.
Mit AIR 2.6 erhält eine Neuheit Einzug in die Flash Plattform: die nativen Mauscursor. Sie werden von den Runtimes direkt mithilfe des Betriebssystems umgesetzt, was einen Performanceverbesserung mit sich bringt.
Ob native Mauscursor möglich sind, kann man mit der Eigenschaft Mouse.supportsNativeCursor
überprüfen. Die maximale Größe eines Cursors darf 32×32 Pixel betragen. Laut Doku sind Transparenzen auf dem meistens Betriebssystemen möglich.
Auch animierte Mauscursor können leicht erstellt werden. Dazu fügt man einfach die verschiedene Animationsphasen des Cursors als Bitmapdaten hinzu und stellt eine Framerate ein (MouseCursorData.frameRate
).
Außerdem lässt sich definieren, auf welchem Punkt der Grafik/Animation die Mausspitze sitzen soll (MouseCursorData.hotSpot
). Der Defaultwert ist die linkere obere Ecke (0/0).
Beispiel
[kml_flashembed publishmethod=“dynamic“ fversion=“9.0.0″ movie=“/wp-content/uploads/2011/04/native-mouse-cursor-air.swf“ width=“500″ height=“500″ targetclass=“flashmovie“/]
Quellcode
[as]
package
{
import flash.display.*;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.ui.Mouse;
import flash.ui.MouseCursorData;
import flash.events.MouseEvent;
import flash.text.TextField;
[SWF(backgroundColor=“0xEEEEEE“)]
public class NativeCursorExample extends Sprite
{
[Embed(source=“custom.png“)] private var CustomCursorClass:Class;
private static const CURSOR_WIDTH:uint = 32;
private static const CURSOR_HEIGHT:uint = 32;
private var CUSTOM_CURSOR_NAME:String = „sandclock“;
private var mcd:MouseCursorData;
public function NativeCursorExample()
{
checkSupportForNativeCursors();
stage.addEventListener(MouseEvent.CLICK, onClick);
addCursor();
}
private function addCursor():void {
// neues Vectorobjekt für Bitmaps anlegen
var bitmaps:Vector.
// Bitmapdaten-Object aus dem PNG erstellen
var customCursor:Bitmap = new CustomCursorClass();
// neues Rechteck in Cursorgröße erstellen
var rect:Rectangle = new Rectangle(0 , 0, CURSOR_WIDTH, CURSOR_HEIGHT);
// Dem Bitmap-Vektor hinzufügen
bitmaps.push(customCursor.bitmapData);
// neues MausCursorData-Objekt
mcd = new MouseCursorData();
mcd.data = bitmaps;
// Hot Spot der Maus ändern, da standardmäßig auf 0/0
mcd.hotSpot = new Point(16, 16);
// Framerate auf 0, da keine Animation
mcd.frameRate = 0;
// Cursor registrieren
Mouse.registerCursor(CUSTOM_CURSOR_NAME, mcd);
}
private function onClick(evt:MouseEvent):void {
if (Mouse.cursor == CUSTOM_CURSOR_NAME) {
// standard cursor
Mouse.cursor = flash.ui.MouseCursor.AUTO;
} else {
// custom cursor
Mouse.cursor = CUSTOM_CURSOR_NAME;
}
}
private function checkSupportForNativeCursors():void {
// neues Textfeld
var txt:TextField = new TextField();
txt.width = 300;
txt.x = txt.y = 10;
// Fehlermeldung oder Hinweis
Mouse.supportsNativeCursor ? txt.text = „Please click stage to change the cursor.“ : txt.text = „Native mouse cursors are not supported“;
//Textfeld auf die Bühne
addChild(txt);
}
}
}
[/as]
Link: Native Cursors in AIR 2.6
Link: MouseCursorData-Klasse (ActionScript-Referenz)
[ad]