RPG-Inventory - Drag and Drop

  • 54 favourites



10,422 visits, 24,314 views



This tutorial hasn't been translated.


This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Let's get started

To create all functions we need, we only have to use two different sprites, and my favorit, two different arrays. The sprites represent the Inventory Slots and the Inventory Items.

The arrays will be used to map certain numbers with the "names" of the spriteanimations.

These arrays get initialized at the start of the layout. As you may notice, itemAnim is a two dimensional array. It maps the animation names and the acceptance bits to the decimal value.

Drag and Drop

The drag and drop control is rather tricky. Let us look at the easiest part to begin with. The control of the mouse cursor look:

As you can see, the inventoryItem has an instance variable called drag. Let us take this as a reason to take a closer look at the inventoryItem:

The instance variables in detail:

Acceptable - the binary mask of where to alow the item to placed.

lastX and lastY - the X and Y coordinates of the last slot this item was in.

Destroy - the flag that will be set, if the item is dropped in the bin.

drag - the flag that is set, when the item is dragged.

Now, what happens if an item is picked up:

Actually, this should be pretty straight forward. The only unknown is buffer.

buffer is a global variable that holds the result of the slot check, ie. if the item may be dropped in the slot.

You now may ask, why do we need to check a drop issue at pick time? buffer will be 0 (zero) if the inventorySlot we are trying to pick, holds an inventoryItem and if we already have an item picked - this is to prevent, that more than one item is dragged at the same time.


  • Order by
Want to leave a comment? Login or Register an account!