Clicking/touching and dragging sprites around the screen

Hi All,

Anyone know any examples where you touch/click and drag sprites to move them about the screen.

What would be the best way to do this if I have over 100 sprites on screen? Can they all receive input focus?

Many thanks, T

I would implement this by having a main game object that has a list of all of the game objects containing the sprites. The main game object would have input focus. You cannot have 100+ objects with input focus at the same time (I think the limit is 16). It would be this main game object that takes care of input handling and checks which of the game objects to pick up and drag around.

You can detect which game object that was touched by checking the distance between the touch/mouse pointer and the game objects and if the distance is below some threshold (the size of the sprite) you consider it as a potential touch target. Then maybe also sort by depth.

Another approach would be to have a small collision object follow the mouse and let it detect collisions with the game objects (if they also have collision objects).

I have an example of the latter here: https://github.com/britzl/publicexamples/tree/examples/click_and_drag and live example here: http://britzl.github.io/publicexamples/click_and_drag/index.html

1 Like

Thanks, that makes sense, I’ll give it a try.