LibGDX - Items and itembox?

Hi,

In the first picture you can see 5 images (crown, book, key, sword, heart) and a red circle (the red circle shows the touch on the screen).

I show the 5 images with SpriteBatch on the Screen.

Picture 2: Now, when I touch on the crown and swipe the finger on the screen, I want to move the crown. How can I do this? What’s the keyword for that?

And when my finger is in the near of the itembox, it automatically shows a transparent image of the selected Item.

Picture 3: And now, when I move my finger to the right, the book item moved to the left automatically.

Picture 4: Now when I release my finger (look picture 3 for the position), the crown item snaps in the item box. How can I do this? Where can I find snap algorithms. What are they called?

Are there good tutorials or open source code snippets for that?

Thanks!

This is a typical programming problem where you will need to come up with a solution using some simple logic. :wink:

For example:

  • When the user first clicks the actor, start accepting drag events. Keep hold of the original position on the ground. Move the actor based on the drag.
  • When the drag is over the inventory slot, toggle the slot to show a transparent version of whatever item is being dragged. When the drag moves out of the inventory slot, hide the transparent icon.
  • When the drag event ends: if the item is over a slot, place it in that slot. Otherwise leave it to its original place on the ground.

You can accomplish this with drag and drop handling with scene2D:

Or with DragListener:
http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/utils/DragListener.html

Or simply with touchDragged of InputProcessor and custom logic for hit-testing and such:
http://libgdx.l33tlabs.org/docs/api/com/badlogic/gdx/InputProcessor.html

Thank You! That helped me :slight_smile: