There are lots of different ways of doing that, depending on how complicated you want the item effects to be.
Let’s stay really really simple, we’re going to define 3 different types of objects : a cherry, a lime and a blueberry.
The actual logic of the objects is going to be handled in the world (or player) class; you’ll see what I mean by that in a second. If you had more complicated objects you’d use a different approach (subclassing an Item class for each type).
So, we’re going to have this class for item descriptions and images:
public class Items {
public static const ITEM_TYPE_NONE:int = 0;
public static const ITEM_TYPE_CHERRY:int = 1;
public static const ITEM_TYPE_LIME:int = 2;
public static const ITEM_TYPE_BLUEBERRY:int = 3;
public static const ITEM_TYPE_COUNT:int = 4; // Last one
public static const itemNames:Vector.<String> = Vector.<String>(["Nothing", "Cherry", "Lime", "Blueberry"]);
public static var pickupsBmp:BitmapData;
public static var inventoryBmp:BitmapData;
// Static initialization code to create graphics
{
var s:Sprite = FP.sprite, g:Graphics = s.graphics;
// Create dots for pickups
pickupsBmp = new BitmapData(32 * 4, 32, true, 0);
g.clear();
g.beginFill(0xFF0000); g.drawCircle(32 * 0 + 16, 16, 16); g.endFill();
g.beginFill(0x00FF00); g.drawCircle(32 * 1 + 16, 16, 16); g.endFill();
g.beginFill(0x0000FF); g.drawCircle(32 * 2 + 16, 16, 16); g.endFill();
pickupsBmp.draw(s);
// Create rectangles for inventory
inventoryBmp = new BitmapData(38 * 4, 38, false /*true*/, 0);
g.clear();
g.beginFill(0xFF0000); g.drawRect(38 * 0, 0, 38, 38); g.endFill();
g.beginFill(0x00FF00); g.drawRect(38 * 1, 0, 38, 38); g.endFill();
g.beginFill(0x0000FF); g.drawRect(38 * 2, 0, 38, 38); g.endFill();
inventoryBmp.draw(s);
}
}
Now a couple of changes to the Inventory
and Pickup
classes to handle the graphics
// Only showing changes; the rest stays as it is
public class Pickup extends Entity {
public function Pickup(x:Number, y:Number, itemType: int) {
super(x, y);
this.itemType = itemType;
width = 32; height = 32; // size of the object on screen
type = "pickup";
// Sort out graphic here...
var sprite = new Spritemap(Items.pickupBmp);
sprite.frame = itemType;
this.graphic = sprite;
}
}
public class Inventory extends Entity {
public function Inventory() {
tilemap = new Tilemap(Items.inventoryBmp, TILE_WIDTH*Items.ITEM_TYPE_COUNT, TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT);
}
override public function update():void {
...
if (itemType) {
trace("Clicked on item type " + itemType = " at slot #" + (slotRow*ITEM_COLS+slotCol));
// Do something with the item here
switch(itemType) {
case Items.ITEM_TYPE_CHERRY:
trace("Ugh, a cherry stone!"); break;
case Items.ITEM_TYPE_LIME:
trace("Well I'm not getting scurvy!"); break;
case Items.ITEM_TYPE_BLUEBERRY:
trace("Mmmm, antioxidants!"); break;
}
// And remove the item from inventory since it's now used
tilemap.setTile(slotCol, slotRow, 0);
}
...
}
}
And now the world:
// ...
// Add 30 RANDOM pickups at random locations
for (var i:int = 0; i < 30; i++) {
// The 1 is the item type... probably best to use constants for this
add(new Pickup(FP.rand(FP.width-32), FP.rand(FP.height-32), 1 + FP.rand(Items.ITEM_TYPE_MAX - 1));
}
// ...
if (inventory.addItem(itemType)) {
// Fine, item added - remove the pickup from the world
world.remove(this);
// Display debug message
trace("You picked up a " + Items.itemNames[itemType]);
}
There are lots of other ways of doing this! This is appropriate for simple objects, but for something like a roguelike or point 'n click adventure where objects can be combined and have complex behaviours, you’d need something more in-depth.