PDA

View Full Version : Selecting objects in a 2d game



Traveler
22-02-2006, 03:58 PM
I'm having a bit of trouble with my gui and the other elements in my 2D game. Currently I'm able to select every object using my mouse. Whenever I hover over something, a lifebar appears, and where applicable, I can perform actions.

The problem is not that difficult, and I already have sort of a solution but I was interested if other solutions might come up.

Here's the thing: how I do prevent sprites or gui elements to perform an action when both happen to be under my mousepointer at the same time?

In my solution I figured that gui elements always come first. Plus, in my game it is not really needed for sprites to walk underneath gui elements while having the need for a selection, so I can kinda skip those.

With sprites versus sprites I can look up the one that's on top, so that kinda solves it too.

Hmm it's actually pretty simple now that I have written all this. :scratch:
oh well...

Any other thoughts are welcomed too of course :)

savage
22-02-2006, 04:54 PM
I agree, GUI elements take precedence. If you use a Z order you could use that.

cairnswm
23-02-2006, 04:31 AM
My rule is always that events stop happening once the first has been triggered. So even if two sprites are on top of one another an event will only hapen for the top one.

Traveler
23-02-2006, 08:15 AM
How does that work? Do you have a global variable that's set to false once an event has been triggered or something?

Still, in your case you still have to look for what's on top or not right?

cairnswm
23-02-2006, 08:39 AM
I typically have everything in an ordered list. (Things like a GUI thats always on top is always at the bottom of the list). When I search for an item in the list based on something like mouse positin I return the object and exit the search process.

If you look at my game designs I typically have a MAP object that controls the game screen. Within the MAP is a items list linking all the game objects. So when the SCREEN has an event it requests an object from the MAP - and there can only be one return object - ie only one object can be affected.

Just be aware that this does have other problems - the biggest one to me is the AI of each object that typically needs to react to multiple other objects on the MAP. Sometimes its difficult to get links to all the other objects.

User137
23-02-2006, 07:27 PM
GUI is a bit different when it comes for example to selecting units in a strategy game. I usually make a loop of shown units that calculates which one's center point is nearest to the cursor, while cursor has to be closer than unit radius. That way it always clicks the unit you are pointing at.


Selected:=-1; Nearest:=9999;
for i:=0 to UnitCount-1 do
with unit[i] do begin
temp:=hypot(mouseX-X,mouseY-Y);
if &#40;temp<Nearest&#41; and &#40;temp<=Radius&#41; then begin
Selected&#58;=i; Nearest&#58;=temp;
end;
end;
// And here our Selected variable is index to closest unit to cursor
// if it is greater than or equal to 0 ...

Traveler
23-02-2006, 08:19 PM
An interesting suggestion! ill keep that one in mind.