开发者

Trouble with custom cursor in ActionScript 3.0

I wrote a simple game and I want to add custom pointer. I created MovieClip called Pointer, exported it to AS3 and wrote this code:

var pointer:Pointer = new Pointer();
pointer.scaleX=0.1; //that's because cursor turned to be MUCH bigger than needed
pointer.scaleY=0.1;
stage.addEventListener(MouseEvent.MOUSE_MOVE, redrawCursor); 
stage.addEventListener(Event.MOUSE_LEAVE, hideCursor); 
Mouse.hide(); 
function redrawCursor (event:MouseEvent):void { 
    pointer.开发者_C百科visible = true; 
    pointer.x = event.stageX; 
    pointer.y = event.stageY; 
} 
function hideCursor (event:Event):void { 
    pointer.visible = false; 
} 

I suppose there's nothing to explain -- code is too simple. In the game, on frame 74 some objects are created on the stage. If I paste this code BEFORE generating and adding other MovieClip instances, cursor is actually BEHIND these objects. If I paste this code AFTER, mouse is on top, but MouseListeners don't react at all. What's the problem? :SS


Every time you use the addChild method it puts the DisplayObject on top of everything else in the target's display list.

Either make two DisplayObjects, put mouse into the top one and everything else into the bottom one or use addChildAt method to put whatever under your mouse.

EDIT: Normal following cursors are very laggy and usually don't give such a good user experiance. If you can use flash player 10.2+ you should use a native mouse cursor.

var cursorData:MouseCursorData = new MouseCursorData();
var bitmapDatas:Vector.<BitmapData> = new Vector.<BitmapData>();
var bitmap:Bitmap = new zoomCursor();
bitmapDatas[0] = bitmap.bitmapData;
cursorData.data = bitmapDatas;
Mouse.registerCursor("myCursor", cursorData);
Mouse.cursor = "myCursor";


Add a container to your movie at the very top. If not sure use:

var container : Sprite = new Sprite();
container.name = "pointer";
stage.addChild(container);

This should work to have your pointer placed at the very top at all times. This is because your main application is by default the first (and the only) child of the stage. Adding the container to the stage will place it right above the main application.

Add your pointer to this container.

Sprite(stage.getChildByName("pointer")).addChild(pointer);

Disable mouse interactivity of your pointer. Otherwise it could swallow your mouse clicks and they never reach the movie clips underneath.

pointer.mouseEnabled = false;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜