Page 1 of 1

Creating an event based environment

PostPosted: Sun Dec 29, 2013 8:39 pm
by Stormy
So I am fucking around with playmycode.com and my own javascript canvas crap and I am trying to create objects which fire off functions when they are clicked. Right now I am checking the position and bounds of every single object on screen whenever the user clicks the mouse in order to see what they are most likely clicking. It doesn't always work (I have to implement a z-buffer sorting thingy to fix it up a bit) and I know it will get a lot more inefficient as I add more objects to be clicked.

It would be silly to ask for a specific solution, rather I want to know the theory behind creating event-based objects. I really don't like polling every single object on screen each time the user clicks the mouse. Can I assign a property to each pixel when it is drawing a specific object (like a big array, one entry for each pixel) and have the mouse click location just check that array? Should I divide the screen up into smaller areas so I don't have to check as much at once? How is this usually handled?

Re: Creating an event based environment

PostPosted: Sun Dec 29, 2013 10:07 pm
by zombie@computer
Black_Stormy wrote:So I am fucking around with playmycode.com and my own javascript canvas crap and I am trying to create objects which fire off functions when they are clicked. Right now I am checking the position and bounds of every single object on screen whenever the user clicks the mouse in order to see what they are most likely clicking. It doesn't always work (I have to implement a z-buffer sorting thingy to fix it up a bit) and I know it will get a lot more inefficient as I add more objects to be clicked.

It would be silly to ask for a specific solution, rather I want to know the theory behind creating event-based objects. I really don't like polling every single object on screen each time the user clicks the mouse. Can I assign a property to each pixel when it is drawing a specific object (like a big array, one entry for each pixel) and have the mouse click location just check that array? Should I divide the screen up into smaller areas so I don't have to check as much at once? How is this usually handled?

On the Net (ie html) all objects are hierarchical. Thus, when you click something, the body is polled. For every child in body, that child is polled. For every child under the click event, each of that childs' objects are polled. Etc. Since on average objects are pretty nicely dispersed over the screen one rarely polls a ginormous amount. Still, depending on the environment, you may want to do some pre-selection using binary space partitioning or similar methods. It depends on your needs really. Just remember to poll squares only. If you want to do additional (ie per pixel) do it AFTER you've checked the square boundingbox.