Flixel Forums

development => help => Topic started by: steve06 on Tue, May 10, 2011

Title: Flixel mouse on top of Dynamic objects
Post by: steve06 on Tue, May 10, 2011
I added in a menu created in flash and added it to the screen like so:

Code: [Select]
mymenu = new Select_Menu();
mymenu.x = 290;
mymenu.y = 220;
FlxG.stage.addChild(mymenu);

This is in my menustate, however when I do this the mouse is on a layer below the mymenu object.  Is there a way to get it to always be ontop?
Title: Re: Flixel mouse on top of Dynamic objects
Post by: photonstorm on Tue, May 10, 2011
You'd have to do this:

Code: [Select]
FlxG._game.addChildAt(mymenu, FlxG._game.getChildIndex(FlxG._game._mouse));

But sadly the _game._mouse is only available within FlxG.

So I'd suggest you either hack a new function into FlxG to do the above (passing it your display object) - or if you only ever have a fixed (non-changing) number of cameras in your game then you can assume what the display index will be and add it directly there (each camera = a new object on the display list).
Title: Re: Flixel mouse on top of Dynamic objects
Post by: GrimPanda on Thu, Jul 14, 2011
You'd have to do this:

Code: [Select]
FlxG._game.addChildAt(mymenu, FlxG._game.getChildIndex(FlxG._game._mouse));

But sadly the _game._mouse is only available within FlxG.

So I'd suggest you either hack a new function into FlxG to do the above (passing it your display object) - or if you only ever have a fixed (non-changing) number of cameras in your game then you can assume what the display index will be and add it directly there (each camera = a new object on the display list).

Would it be possible to get an example of this?  I'm having the exact same issue, and I'm really not understanding how this would work... 

EDIT: Also, I don't mind using the flash system mouse at all, but it seems that when I use that, my FlxButtons don't receive a mouse event from them.  So they are 'unclickable'.  Either fix would be great, whichever is easier.
Title: Re: Flixel mouse on top of Dynamic objects
Post by: zadvornykh on Sun, Jul 17, 2011
Flixel uses the position of the mouse sprite for the actual click handing, so the native mouse will not work.

As for the camera's, each one is a separate display object (bitmap) on the stage, and the mouse sprite is placed on top of those.

If you get the child index of the camera, and add your own display object (movieclip) to that index, the child index of the mouse will go up, and your movieclip will be under it.

It is important to note that the flixel mouse is not actually a mouse cursor, but a display object that follows the mouse position.

Come to think of it, it might be somewhat difficult to get your own movieclips to react to it, but that is not something i have worked with.
Title: Re: Flixel mouse on top of Dynamic objects
Post by: Chelnok on Sun, Jul 17, 2011
I had this problem, and as far as i remember, my easy fix was:

pseudoCode:

if (menu.swf active) FlxMouse.hide, flashMouse.show
else flxmouse.show, flashMouse.hide
Title: Re: Flixel mouse on top of Dynamic objects
Post by: GrimPanda on Tue, Jul 19, 2011
If you get the child index of the camera, and add your own display object (movieclip) to that index, the child index of the mouse will go up, and your movieclip will be under it.


Attempting to trace this value yielded nothing but errors, so clearly I'm doing something wrong :)

Code: [Select]
trace("Camdex: " + FlxG.stage.getChildIndex(FlxG.camera));

-or-

trace("Camdex: " + FlxG.stage.getChildIndex(FlxG.camera as DisplayObject));

#1 Errors with "Implicit coercion of a value of type org.flixel:FlxCamera to an unrelated type flash.display:DisplayObject" at compile.
#2 Errors with "Child Parameter must be non-null" (but hey, did I really think that dirty type cast would work out?)

Assuming I could get the index, I'm guessing I would then do something like this (with working code for the index):

Code: [Select]
var myMC:MovieClip;
FlxG.stage.addChildAt(myMC = new MovieClip(), FlxG.stage.getChildIndex(FlxG.camera));
myMC.addChild(foo); myMC.addChild(bar);

So I think I'm on the right track... ( or hope so ), but I still need a bit more hand holding on this code if that's ok?

Title: Re: Flixel mouse on top of Dynamic objects
Post by: GrimPanda on Thu, Jul 21, 2011
Anyone that can help with this?
Title: Re: Flixel mouse on top of Dynamic objects
Post by: photonstorm on Fri, Jul 22, 2011
Try this:

Code: [Select]
FlxG.stage.getChildIndex(FlxG.camera.getContainerSprite());
Title: Re: Flixel mouse on top of Dynamic objects
Post by: GrimPanda on Fri, Jul 22, 2011
Try this:

Code: [Select]
FlxG.stage.getChildIndex(FlxG.camera.getContainerSprite());

Hey Rich,

That gives me the same error as before:

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.


This is where I'm stuck.  I don't know enough about the AS language to understand what's going on here, and this being a somewhat Flixel specific error in this situation, my usual Googlefoo is weak here.  :o)

EDIT: I'd really love to see this topic solved.  I've seen many many threads with variations on this theme, and I a definitive answer would be great to have on the forums, so rather than kludge my way through it, I figured I'd let one of you pro's nail it down.
Title: Re: Flixel mouse on top of Dynamic objects
Post by: photonstorm on Fri, Jul 22, 2011
Here you go - the attached diagram and following should help explain.

It assumes a standard set-up where you've got a normal AS3 Preloader.

So Stage will have 1 child - Preloader.

Preloader itself will have 1 child - Main (this may change if your preloader has other graphics in it, so trial and error needed here).

Main will have a minimum of 5 children:

Camera (Child Index 0)
Mouse (Child Index 1)
FlxDebugger (Child Index 2)
Sound Tray (Child Index 3)
Focus Screen (Child Index 4)

Note that if your game contains more than 1 camera it's added before Mouse, so a game with 4 cameras would look like this:

Camera 1 (Child Index 0)
Camera 2 (Child Index 1)
Camera 3 (Child Index 2)
Camera 4 (Child Index 3)
Mouse (Child Index 4)
FlxDebugger (Child Index 5)
Sound Tray (Child Index 6)
Focus Screen (Child Index 7)

As the Mouse index value changes if cameras are added / removed the easiest way to get it is to use Main.numChildren - 4 (remember that numChildren starts from 1, but the Display List is zero index based). It's up to you to work out where a reference to Main is created in your game. For example the following works in my test script:

Code: [Select]
var mainRef:Sprite = Sprite(FlxG.stage.getChildAt(0)).getChildAt(0) as Sprite;

But it might not depending on what other things you have in your Preloader!