Click on overlapping objects

Hello, I’ve been figuring out what’s the best way to implement clickable dynamically-created things in my game. I’ve tried collision shapes on GO and on the cursor, and using GUI (leaning towards collision shape because I’m not a fan of keeping everything in a table and looping through them every click).

The problem I have is how do I handle clicking on overlapping things? And ideally only ‘click’ the object on top.

https://britzl.github.io/Defold-Input/

take a look example called: cursor

Thanks. Couldn’t get the library to work on my project but going into the demo I think this is the relevant bit:

		if not self.collision_id or go.get_world_position(self.collision_id).z < message.other_position.z then
			if not self.state.dragging or (self.state.dragging and message.other_id ~= self.state.pressed_id) then
				self.collision_id = message.other_id
				self.collision_group = message.other_group
			end
		end

So if I understand correctly I can use go.get_world_position to get their on-screen z position, which I can then compare to get the one on top.

Yes, that’s correct. Use the z-value to pick the top one if you are colliding with multiple things in the frame when the user clicks.

2 Likes