i recently started picking up flash again and i thought following a tutorial would be nice for making a inventory system for a new simple game concept.
i found something on yt and i came out with the following code:
var slots:Array = []; // inv sloten
var deck:Array = []; // kaarten in deck
var objects:Array = []; //kaarten in collection
var inventory;
var menu1:menu = new menu ;
var menuOpen = false;
var beforeX;
var beforeY;
function init()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
mLevel.text = "10";
hLevel.text = "10";
var b = 0;
for(var i=0;i<10;i++)
{
var inv:invSlot = new invSlot();
slots.push(inv);
if(i>=5)
{
slots[i].x = 510-50;
slots[i].y = 50*b+15
b++;
}else
{
slots[i].x = 510;
slots[i].y = 50*i+15
}
slots[i].id = i;
slots[i].used = false;
}
for (var j=0; j<10; j++)
{
var item = ((j < 5) ? new mana() : new health());
addChild(item);
objects.push(item);
objects[j].id = j;
objects[j].type = ((j < 5) ? "mana" : "health");
objects[j].x = 200;
objects[j].y = 40 * j;
objects[j].moving = false;
objects[j].addEventListener(MouseEvent.CLICK, checkForClicks);
objects[j].buttonMode = true;
}
trace(slots);
trace(objects);
trace(item);
trace(inv);
}
function keyPressed(event:KeyboardEvent):void
{
var key:uint = event.keyCode;
switch(key)
{
case Keyboard.LEFT:
Character.x -= 5;
checkForHits();
break;
case Keyboard.RIGHT:
Character.x += 5;
checkForHits();
break;
case Keyboard.UP:
Character.y -= 5;
checkForHits();
break;
case Keyboard.DOWN:
Character.y += 5;
checkForHits();
break;
case 73:
if(menuOpen)
{
menuOpen = false;
closeInv();
}
else
{
menuOpen = true;
openInv();
}
break;
}
}
function openInv()
{
addChild(menu1);
menu1.moveMode.addEventListener(MouseEvent.CLICK, moveMode);
for each (var inv in slots)
{
addChild(inv);
inv.visible = true;
setChildIndex(inv, numChildren - 1);
}
for each (var item in objects)
{
if (item.inInventory == true && item.used != true)
{
addChild(item);
item.visible = true;
item.x = slots[item.invid].x;
item.y = slots[item.invid].y + 22.5;
setChildIndex(item, numChildren - 1);
}
}
}
function closeInv()
{
removeChild(menu1);
for each (var inv in slots)
{
removeChild(inv);
inv.visible = false;
}
for each (var item in objects)
{
if (item.inInventory == true && item.used != true)
{
removeChild(item);
item.visible = false;
}
}
}
function checkForHits()
{
var newestFree:Number = 0;
for each (var item in objects)
{
if (Character.hitTestObject(item) && item.inInventory != true)
{
while (newestFree <= slots.length-1)
{
if (slots[newestFree].used == true)
{
if (newestFree == slots.length-1)
{
break;
}
newestFree++;
}
else
{
slots[newestFree].item = item;
item.inInventory = true;
if (!menuOpen)
{
removeChild(item);
}
else
{
setChildIndex(item, numChildren - 1);
}
item.x = slots[newestFree].x;
item.y = slots[newestFree].y + 22.5;//spawn in the middle of itemslot
slots[newestFree].used = true;
slots[newestFree].holding = item.type;
bindItem(newestFree, item.id);
objects[item.id].addEventListener(MouseEvent.CLICK, clickObj);
break;
}
}
}
}
}
function checkForClicks(event:Event)
{
var newestFree:Number = 0;
for each (var item in objects)
{
if (item == event.currentTarget && item.inInventory != true)
{
while (newestFree <= slots.length-1)
{
if (slots[newestFree].used == true)
{
if (newestFree == slots.length-1)
{
break;
}
newestFree++;
}
else
{
slots[newestFree].item = item;
item.inInventory = true;
if (!menuOpen)
{
removeChild(item);
}
else
{
setChildIndex(item, numChildren - 1);
}
item.x = slots[newestFree].x;
item.y = slots[newestFree].y + 22.5;//spawn in the middle of itemslot
slots[newestFree].used = true;
slots[newestFree].holding = item.type;
bindItem(newestFree, item.id);
objects[item.id].addEventListener(MouseEvent.CLICK, clickObj);
break;
}
}
}
}
}
function clickObj(event:Event):void
{
var oid = event.target.id;
var id = slots[objects[oid].invid].id;
var item = objects[oid];
trace(oid)
trace(id)
useObj(id, item, slots[id].holding);
item.removeEventListener(MouseEvent.CLICK, clickObj);
}
function useObj(invId, item, objectType)
{
switch (objectType)
{
case "mana" :
removeChild(item);
mLevel.text = String(int(mLevel.text)+5);
slots[invId].used = false;//zegt dat het invSlot niet langer in gebruik is
item.visible = false;
item.used = true;
//item.inInventory = false;
unbindItem(invId, item.id);
break;
case "health" :
removeChild(item);
hLevel.text = String(int(hLevel.text)+5);
slots[invId].used = false;
item.visible = false;
item.used = true;
//item.inInventory = false;
unbindItem(invId, item.id);
break;
}
}
function bindItem(invId, bindId) //bindId = de combinatie van de slotplek+item
{
slots[invId].itemid = bindId;
objects[bindId].invid = invId;
slots[invId].holding = objects[bindId].type
}
function unbindItem(invId, bindId)
{
slots[invId].itemid = bindId;
slots[invId].itemid = null; //het bindId word gecleared
objects[bindId].invid = null; //het invId word gecleared
slots[invId].holding = null; //het type mana ofhealth word gecleared
}
function checkBind(invId)
{
if (slots[invId].itemid != null)
{
return true;
}
else
{
return false;
}
}
function moveMode(event:Event):void
{
var item;
if (menu1.movemode)
{
menu1.moveMode.gotoAndStop(1);
menu1.movemode = false;
for each (item in objects)
{
if (item.invid != null)
{
item.removeEventListener(MouseEvent.MOUSE_DOWN, moveItem);
item.removeEventListener(MouseEvent.MOUSE_UP, dropItem);
item.addEventListener(MouseEvent.CLICK, clickObj);
}
}
}
else
{
menu1.moveMode.gotoAndStop(2);
menu1.movemode = true;
for each (item in objects)
{
item.removeEventListener(MouseEvent.CLICK, clickObj);
item.addEventListener(MouseEvent.MOUSE_DOWN, moveItem);
item.addEventListener(MouseEvent.MOUSE_UP, dropItem);
}
}
}
function moveItem(event:Event):void
{
beforeX = event.target.x;
beforeY = event.target.y;
event.target.startDrag(true);
event.target.moving = true;
}
function dropItem(event:Event):void
{
var oldInvId:Number = event.target.invid;
var bindId:Number = event.target.id;
event.target.stopDrag();
if (event.target.moving != true) //!= ofwel niet true
{
for each (var item in objects)
{
if (item.moving == true)
{
item.x = beforeX;
item.y = beforeY;
item.moving = false;
break;
}
}
}
else // als movement mode dus wel aanstaat
{
var getObj:String = event.target.dropTarget.parent.toString();
if (getObj == "[object invSlot]")
{
var invId:Number = event.target.dropTarget.parent.id;
if (checkBind(invId))
{
//if this inventory returns treu, than it is already used
objects[bindId].x = beforeX;
objects[bindId].y = beforeY;
objects[bindId].moving = false;
}
else
{
var newX = event.target.dropTarget.parent.x;
var newY = event.target.dropTarget.parent.y;
unbindItem(oldInvId, bindId);
slots[oldInvId].used = false;
bindItem(invId, bindId);
slots[invId].used = true;
objects[bindId].used = false;
objects[bindId].x = newX;
objects[bindId].y = newY + 22.5;
objects[bindId].moving = false;
objects[bindId].id = bindId;
}
}
else
{
event.target.x = beforeX;
event.target.y = beforeY;
}
}
}
init();
it all works but its far from perfect, for example the "items" get defined by type eather health/mana so that limits it.
also theres a bug where you can drag your items on the stage instead of in the inventory.
i like to make a simple picture game where you pick 4 pictures out of 10 from a collection into your inventory and after you can link them on a defined location on the stage.
i would like to use a array for the pictures since that makes me able to sort them easy.
anyway any advise on improving my script or simplyfing it would be more than welcome!