简体   繁体   中英

What's an efficient way to test if a player has clicked on one of many objects in a Java game?

This is a basic question, but since I haven't been coding Java for long I'd like to get an opinion from someone with more experience. Let's say I have a game in Java in which there are many objects (on the order of 30) on the screen. The player can click on an object and each object handles a mouse click differently.

My question is: if the player clicks on the game window, what's a good way to test if he clicked one of the objects, and if so, trigger that object's mouse click event handler?

Here's what I've come up with so far: my applet has a mouse listener and it could also have a list of all game objects currently in existence. The applet's event handler could traverse the list of objects and see if the coordinates of the click are inside that object's bounding box through something like gameObject.isInsideBBox(int mouseX, int mouseY) .

This would be simple to set up, but I feel like it would be inefficient. If the number of objects is large then the program would have to traverse the list every time a user clicks. Couldn't this become a performance issue?

One alternative would be to have each object have its own MouseListener and add all the objects to some sort of global container. Would this method be more efficient in terms of performance than the above? One other concern is that these Component and Container objects seem to be defined in Java's Swing library, but I'm not using Swing for any other purposes. I have the feeling that would be a misuse of the library.

Any feedback/comments?

(I'd be interested to know if these methods would port over to Android. I realize that's a different topic, but if you have any insight on that, I'd appreciate if you could let me know.)

30 objects on screen is few enough that it shouldn't really matter much. A simple linear search should still be easily fast enough. The only real reason to consider optimizing this would be if the '30' might be way off, and you're really dealing with (say) 1000+ objects.

In that case, one simple possibility would be to sort objects by one dimension so you can do a binary search on that dimension. That should leave only a few objects to search among to find the correct value in the other direction.

As @Jerry Coffin says for a small number of objects, a linear search should be fine.

For large numbers of objects linear search scales badly.

  • If the objects are stationary, you can divide the screen up into a hierarchy of regions; eg a quadtree , and place each object into a leaf region. This allows you to reduce the number of objects you need to check.

  • If the objects are moving rapidly, I don't think that quadtrees would work. Every time you moved an object you'd need to recalculate its position in the quad tree, and that is expensive.

  • For slowly moving objects, quadtrees might work, provided that each object knows what quadtree region it is currently inside. (Check that an object is still in a region is O(1) .)

each object handles a mouse click differently.

each object have its own MouseListener

Given the requirement it sounds like a good solution.

If your game objects derive from a class that implements the Shape interface, one of the conatains() methods may be sufficient. This example can handle thousands of objects using a linear search.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM