问题描述:

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

网友答案:

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;
网友答案:

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";
相关阅读:
Top