code:
class InventoryObject {
public var objType:String; // type: sword, shield, spear, book, scroll etc.
public var objDescription:String; // short desription
private var objOffensive:Number; // damage points when used to attack
private var objDefensive:Number; // points absorbs when used to defense
private var objIdx:Number; // used to generate object's name
// class constructor
public function InventoryObject(whichType:String, objIdx:Number, target_mc:MovieClip) {
switch(whichType) {
case "sword":
this.objType = "sword";
if(Math.random()*100 < 50) {
this.objDescription = "Short sword";
} else {
this.objDescription = "Long sword";
}
this.objOffensive = Math.round(Math.random()*5);
this.objDefensive = Math.round(Math.random()*3);
break;
case "shield":
this.objType = "shield";
if(Math.random()*100 < 20) {
this.objDescription = "Big dwarf shield";
this.objDefensive = Math.round(Math.random()*7);
} else {
this.objDescription = "Small orcish shield";
this.objDefensive = Math.round(Math.random()*5);
}
this.objOffensive = 0;
break;
case "spear":
this.objType = "spear";
if(Math.random()*100 < 50) {
this.objDescription = "Human spear";
this.objOffensive = Math.round(Math.random()*5);
} else {
this.objDescription = "Orcish spear";
this.objOffensive = Math.round(Math.random()*3);
}
this.objDefensive = 0;
break;
}
this.objIdx = objIdx;
trace("new "+this.objType+" created");
showOnStage(target_mc, this.objIdx);
}
// this method used to show object on the stage
// we assume that object type defines the appropriate
// library mc used for object representation
private function showOnStage(targetToAttach:MovieClip, objIdx:Number) {
var newName:String = this.objType+objIdx;
var newDepth:Number = targetToAttach.getNextHighestDepth();
targetToAttach.attachMovie(this.objType, newName, newDepth);
}
}
code:
var inventory_array:Array = new Array();
var inventorySize:Number = 5;
var itemEquipped:Number = -1;
var objTypes_array:Array = new Array();
objTypes_array = ["sword", "shield", "spear"];
addToInventory.onRelease = function():Void {
}
createInventory(inventorySize);
function createInventory(iSize:Number):Void {
trace("done");
for(var i:Number=1; i< iSize; i++) {
drawCell("iCell"+i, i*100, "cell");
this["iCell"+i]._x = i*50;
this["iCell"+i]._y = 0;
}
// create add to inventory btn
drawCell("addToInventory_btn",
this.getNextHighestDepth(),
"add2Inv");
this["addToInventory_btn"]._x = 0;
this["addToInventory_btn"]._y = 75;
}
function drawCell(cellName:String, cellDepth:Number, textMsg:String) {
trace("drawCell: "+cellName+":"+cellDepth);
if(textMsg == "cell") {
// draw inventory placeholder
this.createEmptyMovieClip(cellName, cellDepth);
this[cellName].beginFill(0xEFEFEF, 30);
this[cellName].lineStyle(1, 0x000000, 100);
this[cellName].moveTo(0, 0);
this[cellName].lineTo(50, 0);
this[cellName].lineTo(50, 50);
this[cellName].lineTo(0, 50);
this[cellName].lineTo(0, 0);
this[cellName].endFill();
this[cellName].onRelease = function() {
itemEquipped = parseInt(cellName.substr(5))-1;
if(_root.inventory_array[itemEquipped].objType != undefined) {
trace("item eqiupped: "+
_root.inventory_array[itemEquipped].objType+":"+
_root.inventory_array[itemEquipped].objDescription);
}
}
} else {
// draw button
this.createEmptyMovieClip(cellName, cellDepth);
this[cellName].beginFill(0xEFEFEF, 30);
this[cellName].lineStyle(1, 0x000000, 100);
this[cellName].moveTo(0, 0);
this[cellName].lineTo(100, 0);
this[cellName].lineTo(100, 17);
this[cellName].lineTo(0, 17);
this[cellName].lineTo(0, 0);
this[cellName].endFill();
this[cellName].createTextField(textMsg, this.getNextHighestDepth(), 0, 0, 100, 17);
if(textMsg == "add2Inv") {
this[cellName].add2Inv.text = textMsg;
this[cellName].onRelease = function() {
trace("this is "+textMsg+" button");
// check if inventory full
if((inventory_array.length + 1) == 5) {
trace("inventory full");
} else {
var objIdx:Number = Math.floor(Math.random()*objTypes_array.length);
var tDepth:Number = inventory_array.length+1;
var target_mc:MovieClip = _root["iCell"+tDepth];
var typeVal:String = objTypes_array[objIdx];
var tmpObj:InventoryObject = new InventoryObject(typeVal, tDepth, target_mc);
inventory_array.push(tmpObj);
}
}
}
}
}