Cursor Below Enemies - Need Help

12 posts

Flag Post

The custom cursor I’m using for my game is always below spawned objects(the enemies). I don’t know how to fix this. To attach the enemies, I’m using the attachMovie method with getNextHighestDepth. I was thinking about using the swapdepths method, but I have no idea on how to use that. The cursor is on the stage to begin with. I am using AS2. Please Help.

 
Flag Post

Anti-AS2 comments aside, you can fix this easily by using containers instead of simply tacking everything onto root. So:

// do this only at the beginning when intializing your gamefield.  You should actually probably declare them as private, up at the top (or public if your code is messy)
var mEnemyContainer:MovieClip = _root.createEmptyMovieClip("enemyContainer", _root.getNextHighestDepth());
var mCursorContainer:MovieClip = _root.createEmptyMovieClip("cursorContainer", _root.getNextHighestDepth());

// ...later on

// the cursor will always be above the enemies, no matter how many enemies you try to attach.
var someCrappyEnemy:MovieClip = mEnemyContainer.attachMovie("Goblin", "Goblin" + mEnemyContainer.getNextHighestDepth(), mEnemyContainer.getNextHighestDepth());

 
Flag Post
Originally posted by Aesica:

Anti-AS2 comments aside, you can fix this easily by using containers instead of simply tacking everything onto root. So:

// do this only at the beginning when intializing your gamefield.  You should actually probably declare them as private, up at the top (or public if your code is messy)
var mEnemyContainer:MovieClip = _root.createEmptyMovieClip("enemyContainer", _root.getNextHighestDepth());
var mCursorContainer:MovieClip = _root.createEmptyMovieClip("cursorContainer", _root.getNextHighestDepth());

// ...later on

// the cursor will always be above the enemies, no matter how many enemies you try to attach.
var someCrappyEnemy:MovieClip = mEnemyContainer.attachMovie("Goblin", "Goblin" + mEnemyContainer.getNextHighestDepth(), mEnemyContainer.getNextHighestDepth());

My custom cursor is controlled by frame code. It’s based off of the root. How would I make the previous code work? Also, how would I have the enemies spawn with the previous code, because I’m using the attachMovie method in an external class to attach enemies?

Mouse.hide();
onMouseMove = function()
{
	cursor._x = _xmouse;
	cursor._y = _ymouse;
}
 
Flag Post

Or if you wanted to be lazy and not use containers you could probably do something like

//enter frame
{
cursor._depth = _root.getNextHighestDepth();
}

However, its been eons since I’ve touched AS2 so I could be wrong.

 
Flag Post
Originally posted by CaptianKirk:

My custom cursor is controlled by frame code.

Stop right there.

1) Use classes, for the love of god.
2) Stop attaching ANYTHING to root.

(3: UG’s code, while functional, is a bad thing to do)

 
Flag Post
Originally posted by CaptianKirk:

My custom cursor is controlled by frame code. It’s based off of the root. How would I make the previous code work? Also, how would I have the enemies spawn with the previous code, because I’m using the attachMovie method in an external class to attach enemies?

Mouse.hide();
onMouseMove = function()
{
	cursor._x = _xmouse;
	cursor._y = _ymouse;
}

1) Frame code that does anything more than control the animation flow is positively toxic—even then, I doubt I’d use it. When you go to debug, it’s easy for potential problem code to get lost when you have little pieces here and there in various frames on the main timeline as well as in the timeline of various symbols. Avoid it like the plague, because you really don’t need to use it—classes are 100x better.

2) Tacking everything onto root is bad, bad, bad. Weird shit can happen when you stick everything on root, like numbers can morph into strings or who knows what else. Plus, objects on root offer you little/no autocomplete, making your job harder than it has to be. It also makes depth management a nightmare. Use containers. Use public static vars in a class if you really need everything to have access to it (I hear encapsulation crying somewhere off in the distance as I type this)

3) I’m actually not sure what object “owns” your snippet of code, but I’m going to assume it’s the root timeline. (Eww) Assuming your cursor is _root.cursor, you’d probably reference it as _root.cursorContainer.cursor instead. Personally, I never referenced things through root. It’s bad because, if memory serves, it’s one of those things AS2 will just fail silently if something’s wrong, leading to all sorts of hassle when you go back to try and find the problem. You really need to learn classes.

Edit: @UG: That’s…really not a good idea. It was awhile ago, but in my crappy idle game, when the depth got too high, things slowed to a crawl before finally crashing. To this day, I don’t know (or care) if it was purely because the depth number got too high, or something else related to depth, but the problem magically went away by forcing the depth back down periodically. To force the depth up every frame like that (in addition to anything else that might be getting added: enemies, bullets, etc) means having the depth soar into the tens of thousands in a matter of minutes.

 
Flag Post

The solution would be to put all objects that are created ingame inside a container.
Also, for the custom cursor, use the stratDrag() funtion, instead of changing the x and y every frame.

If you still have to learn about classes in AS2, you might as well skip that, and start with AS3, look on this forum for more info on that.

 
Flag Post
Originally posted by JohnnyBohnny:

Also, for the custom cursor, use the MouseEvent.MOUSE_MOVE event, instead of changing the x and y every frame.

Fixed that for you. Mouse_Move calls immediately, before the frame is drawn, allowing for a 100% accurate mouse positioning. Enter_Frame is called on an internal timer, and will lag slightly behind the mouse (you can see this if you cause the system mouse to be visible at the same time).

(Pro-tip: startDrag() is used when a mouse needs to move another object, such as moving an item between inventory spaces, it is not intended to make custom cursors).
((Even more pro pro-tip: use AS3 and it’s Cursor class))

 
Flag Post

Just poking my nose into this thread to second the container point. If you put all your in-game objects into a container at depth 1, all your HUD/UI items into a different container at depth 2, and your cursor in a third at depth 3, the overall display order will always be what you want.

 
Flag Post
Originally posted by BobJanova:

Just poking my nose into this thread to second the container point. If you put all your in-game objects into a container at depth 1, all your HUD/UI items into a different container at depth 2, and your cursor in a third at depth 3, the overall display order will always be what you want.

You really have to plan that out at the start of your project, it’s a bit late for that now.

 
Flag Post
Originally posted by JohnnyBohnny:
Originally posted by BobJanova:

Just poking my nose into this thread to second the container point. If you put all your in-game objects into a container at depth 1, all your HUD/UI items into a different container at depth 2, and your cursor in a third at depth 3, the overall display order will always be what you want.

You really have to plan that out at the start of your project, it’s a bit late for that now.

What?!

Given the code is somewhat coherently laid out, it is unlikely to take more than an hour to sort this kind of thing out; in fact it’s arguably a five minute job in most cases

The main requirement is that the Flash IDE stage is not used to add objects directly

 
Flag Post

Maybe it is, but if you are referring to other objects a lot, like ‘root.fatCatmc.x’, you have to replace that.

I guess you could just press ctrl+f, actually.