1

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

nicks
  • 2,161
  • 8
  • 49
  • 101
  • Where do you add `pointer` to the stage? – shanethehat Aug 04 '11 at 11:26
  • 1
    +1 for `//that's because cursor turned to be MUCH bigger than needed ` – Kaken Bok Aug 04 '11 at 11:27
  • @shanethehat what do you mean? in actions o.O – nicks Aug 04 '11 at 11:28
  • I mean where, in the code you have provided, do you add the pointer to the stage? I see you create an instance, and set the scale value, but nowhere is there something like `this.addChild(pointer);` – shanethehat Aug 04 '11 at 11:32
  • 1
    Somewhere I read that a newer version of Flash player supports custom cursors. Anyone knows more? – Kaken Bok Aug 04 '11 at 11:34
  • @shanetheahat oh, sure, how could i forget to say. i'm adding cursor AFTER creating all other instances, that's why you can't see it in my code. in fact, it looks something like stage.addChild(pointer); Jens Struwe, never heard about it ; O – nicks Aug 04 '11 at 11:47
  • 1
    They are called Native cursors, and were added in Flash 10.2. Way better than using the old approach, in several ways, not only because of performance. There are several samples and wrappers out there: http://www.adobe.com/devnet/flashplayer/articles/native-mouse-cursors.html - http://toki-woki.net/lab/CustomMouseCursorTests/srcview/ – Neverbirth Aug 04 '11 at 12:03
  • so you mean that it improves performance? sounds great! ; O – nicks Aug 04 '11 at 12:09

2 Answers2

2

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";
Rob Fox
  • 5,355
  • 7
  • 37
  • 63
  • Actually that's what i'm doing. But in this case, as I have said, mouselisteners don't work... – nicks Aug 04 '11 at 11:49
  • If Flash Player 10.2 is of option why not use a native mouse cursor, it has a lot better performance. http://www.adobe.com/devnet/flashplayer/articles/native-mouse-cursors.html otherwise put pointer.mouseEnabled = false; as Jens stated. – Rob Fox Aug 04 '11 at 11:52
  • I added code for creating a native cursor (as modified from http://adobe.ly/eXLDeJ) – Rob Fox Aug 04 '11 at 11:56
  • thank you. but i can't actually see where is the statement that is supposed to indicate to use such and such object for a cursor :S do you mean "myCursor"? :Ss – nicks Aug 04 '11 at 12:03
  • 1
    I cannot tell you how to compile it with your code, just gave you an example. – Rob Fox Aug 04 '11 at 12:22
  • 1
    zoomCursor would be an embedded bitmap. If you use Flex you can embed it like this: [Embed(source="img.png")] private const zoomCursor:Class; If you use Flash IDE you can use the IDE to embed it. – Rob Fox Aug 04 '11 at 12:26
1

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;
Kaken Bok
  • 3,395
  • 1
  • 19
  • 21