Crafting in games takes on many different guises. In this tutorial, I'm going to look at a drag-and-drop style system that's inspired by the game Little Alchemy. In a nutshell, this crafting system allows you to drag one object onto another and if the two objects are compatible, it'll create something new.
To recreate this project, you'll need the following:
Objects
Array – the array object in this project, aCollidingItems, stores data from the overlapping items used in the combining process.
AJAX – required to add the JSON project file to the JSON object.
Browser – this is mostly used for debugging
Item Sprite – the sprites have the Drag and Drop and Bound to Layout behaviors which allow the player to move them around the game screen for 'crafting'. The sprite is also in a family called 'items' and has two instance variables: ID (string) and Used (Boolean).
JSON – the JSON object holds the project file which contains the item data and crafting recipes.
Keyboard – used for spawning new items in the game area
Text – Two text objects show the item list and recipe list at runtime. This is not necessary for the game, but is handy to check your recipes are working properly!
In the layout view, your Text object can be put anywhere as it's just showing some data – you don't actually need it to make the game work. All you really need to add to the layout is a bunch of Item objects. Make sure you set their instance variables and initial animations or else you'll end up with each object being the same item!
Global Variables
The project uses seven global variables to track various bits of data. Starting with tracking the overlaps:
DragItemUID and OverlapItemUID are used to store the UIDs of the item the player is currently dragging and the UID of the item it overlaps when dropped.
The remaining five instance variables ID1, ID1Valid, ID2, ID2Valid and ResultID are involved in the crafting process.