I have a kind of broad question to ask but I have to start some where so.....What is the best way to handle onscreen clicks for moving objects being drawn on a canvas in a view. Im registering the screen clicks just fine and Im able to get the x and y but when it comes to matching it to an object on screen Im at a loss. I have a really hacky way of traversing through an array of existing objects and checking their onscreen coordinates against the coordinates of each item in the array but its no where near reliable and doesnt work most of the time. when it does work the click ends up not matching the item I clicked on. At the moment this all takes place in a custom view and is being drawn on a canvas but in the future Im going to implement it all in opengl when I have the time.....or the money to pay some one else :0)

Any opinions, links to tutorials, or suggestions would be much appreciated thanks.

1 Answer
1

I don't think traversing an array of on-screen objects is too hackish because at some level you're going to need to check each clickable area against each click. And it can really be as simple as just taking an array and testing RectF against x,y coords using RectF.contains(). The alternative is using some space partitioning, like a BSP, but that's almost certainly more than you need.

What might work better for your program, and in particular for Android, is to make a event buffer. You want to keep your processing off the UI thread, so what I've done in the past is make the buffer that acts as a go-between. On the UI thread, the buffer receives incoming events and stores them and that's all. Then in my game thread, when my main loop iterates through the objects, each one just tests against the stored event in the buffer. At the end of the loop, the event in the buffer is cleared.

As for reliability, you might be running into emulator speed issues. The emulator is slow and takes a little while (sometimes like 0.1 seconds for me) to register touch events. By the time the code gets the event, the objects have moved. This part may be easier to debug on a real device. If, on the other hand, you have stationary objects, then it's probably just buggy code. logcat is your bff.

Hey thanks for the info on BSP...thats definitely overkill for my case :) I do have a sort of buffer which is just another Array I keep of UI events but Im not even sure its doing its job. The emulator is not the issue as I test on hardware....I have tablets and phones so its not a hardware specific issue.....when I catch up on some sleep Ill post some of my "hacky" code lol and maybe you can throw in your two cents.
–
James andresakisJan 26 '12 at 14:29

I finally figured that I wasnt re-sorting my objects everytime a group of new ones were created and loaded into the arraylist that I had set up. I had to create a custom comparator and in its onCompare method I set a cascade of if/else statements to put my objects in correct order according to a certain variable value. I also had to play with the math that figured out the bounding box I used to make a clickable area for each object.
–
James andresakisFeb 3 '12 at 5:23