AS: OOP Inventory System
See it
Download source
Inventory Class
Item Class
*note: .as files must be placed in the same folder as the .fla file for everything to work properly
This tutorial covers an Object-Oriented approach to creating an inventory system for your games. If you are interested in making a basic inventory, check out AS: Inventory 1 by Inglor. Also, this tutorial will not teach you Object-Oriented Programming. This tutorial assumes you understand basic OOP concepts such as objects, classes, constructors, encapsulation, etc. If you need an OOP lesson try AS:Basic OOP by Inglor. I also suggest senocular’s tutorial on OOP for actionscript 2.0 if you really want to learn more. Now, let’s move on
Why OOP?
Look, I wont lie to you. There’s a lot of code here. In fact you’ll probably look at it and say ‘wow, I could code that in a quarter of the lines.’ And you would be right. But OOP isn’t about conserving code. Reasons I have chosen to do this in OOP:
-Portability- ideally you should be able to drop this code in place into any flash with a few minimal changes to the graphics. Also, it allows flexibility during the design process. If partway through your game you decide you want your inventory to have six slots in two rows instead of four slots in three rows, you can change it with no hassle.
-Dynamic behavior- this is not a static system. Items are added into the first available slots, not into predetermined places. This lets you fill your inventory in a more realistic manner
-Encapsulation- Most of the dirty work is done by the inventory class. You as the programmer have a set of methods at your disposal. Instead of rewriting lines and lines of code you can simple create an inventory object and call methods such as Inventory.Add(), Inventory.Remove, and Inventory.ToggleVisibility()
Please also note that I am in no way, shape, or form an OOP expert. I only know what I know. In fact I’m anxious to see other programmers’ take on my code
Starting The Inventory
First, we need a movie clip to act as a generic ‘slot.’ This is what each item in your inventory will be placed into. I just used a 75x75 black outline square. Make whatever you like, just make sure it is square. Convert it to a movieclip and set its linkage identifier to “Ibox.” Make sure its registration point is set to the top left. THIS IS IMPORTANT.
Next, you need to draw your inventory items. In my example I drew three simple objects. Whatever you draw make sure they fit within the size of your inventory slot. Convert them to movieclips and give each one a linkage name to remember them by. IE a hammer should be named “hammer,” a knife “knife,” etc. Make sure to set their registration points back to the center. ALSO IMPORTANT.
Now, we’ll jump into our first class, the Item class. Create a new .as file and paste the following code into it:
class Item {
var itemName:String = '';
function Item(inName:String)
{
itemName = inName;
}
}
Remember that .as files must be named the same as the class they represent. The above file MUST be named Item.as or it will simply not work. This is a very basic class and only serves to help us visually see our inventory as a collection of Item objects. This is a class that will be applied to all items, but right now it will remain by itself.