Forkheads
http://www.forkheads.net/

My old inventory system (AS2)
http://www.forkheads.net/viewtopic.php?f=50&t=14644
Page 1 of 1

Author:  Parpol [ Fri Mar 22, 2013 12:34 pm ]
Post subject:  My old inventory system (AS2)

I made an inventory system in AS2 for my platformer a few years ago.
It's unnecessarily complex and not very object oriented, AS2 was being a bitch back then. The code would probably be a lot shorter if I remade it in AS3 but I'm too lazy right now. I'll just let you guys figure it out for yourself. If you have questions about certain parts of the code, you're free to ask questions in this thread and I will answer them.

Code:
var dropped:Boolean = false;
var temp:Object;
var dynamicText:TextFormat = new TextFormat();
var openSlot:Number = 0;
dynamicText.font = "Arial";
dynamicText.bold = true;
dynamicText.size = 12;
Initialize();

function dragItem(_item:Object):Void
{
   //save position before dragging
   _item.nowX = _item._x;
   _item.nowY = _item._y;

   //drag
   _item.startDrag(true);
   _item.onMouseMove = updateDrag;
}
function updateDrag():Void
{
   updateAfterEvent();
}
function canEquip(_item:Object):Boolean
{
   if (_item.levelReq <= _root.lvl)
   {
      return true;
   }
   else
   {
      return false;
   }
}
function switchPlaces(_item:Object, spot:Number)
{
   if (!_root.stackItems)
   {
      var itemSlot = _item._currentSlot;
      _item._currentSlot = spot;
      if (spot < 12)
      {
         trace("The new slot is below 12");
         this["item" + _root.inventory[spot].uniqueItemId]._currentSlot = itemSlot;
         if (itemSlot < 12)
         {
            trace("item you're holding onto is in a slot below 12");
            _root.inventory[itemSlot] = new Object();
            _root.inventory[itemSlot]._id = this["item" + _root.inventory[spot].uniqueItemId]._id;
            _root.inventory[itemSlot].uniqueItemId = this["item" + _root.inventory[spot].uniqueItemId].uniqueItemId;
            _root.inventory[itemSlot]._amount = this["item" + _root.inventory[spot].uniqueItemId]._amount;
            _root.inventory[itemSlot]._currentSlot = this["item" + _root.inventory[spot].uniqueItemId]._currentSlot;
            _root.inventory[itemSlot]._type = this["item" + _root.inventory[spot].uniqueItemId]._type;
         }
         else
         {
            trace("item you're holding onto is in a slot above 12");
            _root.equipment[itemSlot - 12] = new Object();
            _root.equipment[itemSlot - 12]._id = this["item" + _root.inventory[spot].uniqueItemId]._id;
            _root.equipment[itemSlot - 12].uniqueItemId = this["item" + _root.inventory[spot].uniqueItemId].uniqueItemId;
            _root.equipment[itemSlot - 12]._amount = this["item" + _root.inventory[spot].uniqueItemId]._amount;
            _root.equipment[itemSlot - 12]._currentSlot = this["item" + _root.inventory[spot].uniqueItemId]._currentSlot;
            _root.equipment[itemSlot - 12]._type = this["item" + _root.inventory[spot].uniqueItemId]._type;
         }

         _root.inventory[spot] = new Object();
         _root.inventory[spot]._id = _item._id;
         _root.inventory[spot].uniqueItemId = _item.uniqueItemId;
         _root.inventory[spot]._amount = _item._amount;
         _root.inventory[spot]._currentSlot = _item._currentSlot;
         _root.inventory[spot]._type = _item._type;
      }
      else
      {
         trace("The new slot is above 12");
         this["item" + _root.equipment[spot].uniqueItemId]._currentSlot = itemSlot;
         if (itemSlot < 12)
         {
            trace("item you're holding onto is in a slot below 12");
            _root.inventory[itemSlot] = new Object();
            _root.inventory[itemSlot]._id = this["item" + _root.equipment[spot - 12].uniqueItemId]._id;
            _root.inventory[itemSlot].uniqueItemId = this["item" + _root.equipment[spot - 12].uniqueItemId].uniqueItemId;
            _root.inventory[itemSlot]._amount = this["item" + _root.equipment[spot - 12].uniqueItemId]._amount;
            _root.inventory[itemSlot]._currentSlot = this["item" + _root.equipment[spot - 12].uniqueItemId]._currentSlot;
            _root.inventory[itemSlot]._type = this["item" + _root.equipment[spot - 12].uniqueItemId]._type;
         }
         else
         {
            trace("item you're holding onto is in a slot above 12");
            _root.equipment[itemSlot - 12] = new Object();
            _root.equipment[itemSlot - 12]._id = this["item" + _root.equipment[spot - 12].uniqueItemId]._id;
            _root.equipment[itemSlot - 12].uniqueItemId = this["item" + _root.equipment[spot - 12].uniqueItemId].uniqueItemId;
            _root.equipment[itemSlot - 12]._amount = this["item" + _root.equipment[spot - 12].uniqueItemId]._amount;
            _root.equipment[itemSlot - 12]._currentSlot = this["item" + _root.equipment[spot - 12].uniqueItemId]._currentSlot;
            _root.equipment[itemSlot - 12]._type = this["item" + _root.equipment[spot - 12].uniqueItemId]._type;
         }

         _root.equipment[spot - 12] = new Object();
         _root.equipment[spot - 12]._id = _item._id;
         _root.equipment[spot - 12].uniqueItemId = _item.uniqueItemId;
         _root.equipment[spot - 12]._amount = _item._amount;
         _root.equipment[spot - 12]._currentSlot = _item._currentSlot;
         _root.equipment[spot - 12]._type = _item._type;
      }

      setPositions();

      for (var i:Number = 0; i < 12; i++)
      {
      trace("unique ID: " + _root.inventory[i].uniqueItemId + " / " + this["item" + _root.inventory[i].uniqueItemId].uniqueItemId);
      trace("ID: " + _root.inventory[i]._id + " / " + this["item" + _root.inventory[i].uniqueItemId]._id);
      trace("Amount: " + _root.inventory[i]._amount + " / " + this["item" + _root.inventory[i].uniqueItemId]._amount);
      trace("-------------");
      }

   }
}
function dropItem(_item:Object):Void
{
   dropped = false;
   //stop dragging
   _item.stopDrag();

   //delete mouse event
   delete _item.onMouseMove;
   for (var i:Number = 0; i < 12 && !dropped; i++)
   {
      if ((_item._x >= (i % 3 * 40) + 20 && _item._x <= (i % 3 * 40) + 60) && (_item._y >= (Math.floor(i / 3) * 40) && _item._y <= (Math.floor(i / 3) * 40 + 24)))
      {
         if (_item._currentSlot < 12 || (_item._currentSlot >= 12 && (_root.inventory[i]._id == undefined || (_root.inventory[i]._type == _item._type && canEquip(_root.inventory[i])))))
         {
            switchPlaces(_item,i);
            dropped = true;
         }
      }
   }
   for (var i:Number = 0; i < 6 && !dropped; i++)
   {
      if (_item._x > 150 && _item._x < 270 && _item._y < 160)
      {
         if (_item._currentSlot < 12)
         {
            if (canEquip(_item))
            {

               if (_item._type == "Blade")
               {
                  switchPlaces(_item,12);
                  dropped = true;
               }
               else if (_item._type == "Cards" || _item._type == "Throwing stars")
               {
                  switchPlaces(_item,13);
                  dropped = true;
               }
               else if (_item._type == "Neckless")
               {
                  switchPlaces(_item,14);
                  dropped = true;
               }
               else if (_item._type == "Earrings")
               {
                  switchPlaces(_item,15);
                  dropped = true;
               }
               else if (_item._type == "Ring")
               {
                  if (_item._x > 169 && _item._x < 209 && _item._y > 112 && _item._y < 152 || (!(_item._x > 209 && _item._x < 249 && _item._y > 112 && _item._y < 152) && _root.equipment[4] == undefined))
                  {
                     switchPlaces(_item,16);
                     dropped = true;
                  }
                  else if (_item._x > 209 && _item._x < 249 && _item._y > 112 && _item._y < 152 || (!(_item._x > 169 && _item._x < 209 && _item._y > 112 && _item._y < 152) && _root.equipment[5] == undefined))
                  {
                     switchPlaces(_item,17);
                     dropped = true;
                  }
               }

            }
         }
      }
   }
   if (!dropped)
   {
      _item._x = _item.nowX;
      _item._y = _item.nowY;
   }
   setPositions();
}
function setPositions()
{
   for (var i:Number = 0; i < 12; i++)
   {
      this["slot" + (i + 1) + "Amount"].text = "";
      if (this["item" + _root.inventory[i].uniqueItemId]._id)
      {
         this["item" + _root.inventory[i].uniqueItemId]._x = (i % 3 * 40) + 40;
         this["item" + _root.inventory[i].uniqueItemId]._y = (Math.floor(i / 3) * 40 + 12);
         if (_root.inventory[i]._amount > 1)
         {
            this["slot" + (i + 1) + "Amount"].text = _root.inventory[i]._amount;
         }
      }
   }
   this["item" + _root.equipment[0].uniqueItemId]._x = 189;
   this["item" + _root.equipment[0].uniqueItemId]._y = 12;
   this["item" + _root.equipment[1].uniqueItemId]._x = 229;
   this["item" + _root.equipment[1].uniqueItemId]._y = 12;
   this["item" + _root.equipment[2].uniqueItemId]._x = 189;
   this["item" + _root.equipment[2].uniqueItemId]._y = 92;
   this["item" + _root.equipment[3].uniqueItemId]._x = 229;
   this["item" + _root.equipment[3].uniqueItemId]._y = 92;
   this["item" + _root.equipment[4].uniqueItemId]._x = 189;
   this["item" + _root.equipment[4].uniqueItemId]._y = 132;
   this["item" + _root.equipment[5].uniqueItemId]._x = 229;
   this["item" + _root.equipment[5].uniqueItemId]._y = 132;
   //sets every item in your inventory in place.
}

function Initialize():Void
{
   for (var i:Number = 0; i < _root.equipment.length; i++)
   {
      trace("   eq      " + _root.equipment[i]._id);
      if (_root.equipment[i]._id)
      {
         trace(i + " is defined");
         trace("And it has the amount of " + _root.inventory[i]._amount);
         var _item:Object;
         _item = this.attachMovie("item", "item" + _root.uniqueItemId, _root.uniqueItemId);
         _item._id = _root.equipment[i]._id;
         _item.uniqueItemId = _root.uniqueItemId;
         _root.equipment[i].uniqueItemId = _root.uniqueItemId;
         _item._amount = _root.equipment[i]._amount;
         _item._currentSlot = 12 + i;
         _root.setStats(_item);
         drawItem(_item);
         _item.onRollOver = function()
         {
            displayInfo(this);
         };
         _item.onRollOut = function()
         {
            _parent.itemInfo.removeMovieClip();
         };
         _item.onPress = function()
         {
            _parent.itemInfo.removeMovieClip();
            dragItem(this);
         };
         _item.onRelease = _item.onReleaseOutside = function ()
         {
            displayInfo(this);
            dropItem(this);
         };
         _root.uniqueItemId++;
      }
   }
   for (var i:Number = 0; i < _root.inventory.length; i++)
   {
      trace("         " + _root.inventory[i]._id);
      if (_root.inventory[i]._id)
      {
         trace(i + " is defined");
         trace("And it has the amount of " + _root.inventory[i]._amount);
         var _item:Object;
         _item = this.attachMovie("item", "item" + _root.uniqueItemId, _root.uniqueItemId);
         _item._id = _root.inventory[i]._id;
         _item.uniqueItemId = _root.uniqueItemId;
         _root.inventory[i].uniqueItemId = _root.uniqueItemId;
         _item._amount = _root.inventory[i]._amount;
         _item._currentSlot = i;
         _item._x = (i % 3 * 40) + 40;
         _item._y = (Math.floor(i / 3) * 40 + 12);
         _root.setStats(_item);
         drawItem(_item);
         _item.onRollOver = function()
         {
            displayInfo(this);
         };
         _item.onRollOut = function()
         {
            _parent.itemInfo.removeMovieClip();
         };
         _item.onPress = function()
         {
            _parent.itemInfo.removeMovieClip();
            dragItem(this);
         };
         _item.onRelease = _item.onReleaseOutside = function ()
         {
            displayInfo(this);
            dropItem(this);
         };
         _root.uniqueItemId++;
      }
   }
   setPositions();
}
function displayInfo(_item:Object):Void
{
   _parent.createEmptyMovieClip("itemInfo",1);
   _parent.itemInfo._x = _root._xmouse + 10;
   _parent.itemInfo._y = _root._ymouse + 10;
   _parent.itemInfo.lineStyle(1,0x000000,25);
   _parent.itemInfo.beginFill(0x000000,30);
   _parent.itemInfo.moveTo(0,0);
   _parent.itemInfo.lineTo(200,0);
   _parent.itemInfo.lineTo(200,30);
   _parent.itemInfo.lineTo(0,30);
   _parent.itemInfo.lineTo(0,0);
   _parent.itemInfo.endFill();
   _parent.itemInfo.moveTo(0,30);
   _parent.itemInfo.lineTo(200,30);
   var depth:Number = 1;
   var heightStack:Number = 5;
   drawText(_item.__name,10,heightStack,200,20,new Number(0xffffdd),true,depth);
   depth += 2;
   heightStack += 25;
   drawText("Type: " + _item._type,10,heightStack,110,20,new Number(0xf7f7f7),false,depth);
   depth += 2;
   heightStack += 15;
   if (_item._amount != undefined)
   {
      drawText("Amount: " + _item._amount,120,heightStack - 15,80,20,new Number(0xf7f7f7),false,depth);
      depth += 2;
   }
   if (_item.levelReq != undefined)
   {
      var color:Number = new Number(0xffdddd);
      if (_item.levelReq <= _root.lvl)
      {
         color = new Number(0xf7f7f7);
      }
      drawText("Level required: " + _item.levelReq,10,heightStack,110,20,color,false,depth);
      depth += 2;
      heightStack += 15;
   }
   if (_item.CAD != undefined)
   {
      if (_item.CAD < 0)
      {
         color = new Number(0xddddff);
         drawText("Long-range attacks: " + _item.CAD,10,heightStack,190,20,color,false,depth);
      }
      else
      {
         color = new Number(0xccffee);
         drawText("Long-range attacks: +" + _item.CAD,10,heightStack,190,20,color,false,depth);
      }
      depth += 2;
      heightStack += 15;
   }
   if (_item.SAD != undefined)
   {
      if (_item.SAD < 0)
      {
         color = new Number(0xddddff);
         drawText("Melee weapons: " + _item.SAD,10,heightStack,190,20,color,false,depth);
      }
      else
      {
         color = new Number(0xccffee);
         drawText("Melee weapons: +" + _item.SAD,10,heightStack,190,20,color,false,depth);
      }
      depth += 2;
      heightStack += 15;
   }
   if (_item.maxHealth != undefined)
   {
      if (_item.maxHealth < 0)
      {
         color = new Number(0xddddff);
         drawText("Max health: " + _item.maxHealth,10,heightStack,190,20,color,false,depth);
      }
      else
      {
         color = new Number(0xccffee);
         drawText("Max health: +" + _item.maxHealth,10,heightStack,190,20,color,false,depth);
      }
      depth += 2;
      heightStack += 15;
   }
   color = new Number(0xf7f7f7);
   drawText("Slot: " + _item._currentSlot,10,heightStack,190,20,color,false,depth);
   depth += 2;
   heightStack += 15;

   heightStack += 5;
   _parent.itemInfo.beginFill(0x000000,25);
   _parent.itemInfo.moveTo(0,31);
   _parent.itemInfo.moveTo(200,31);
   _parent.itemInfo.lineTo(200,heightStack);
   _parent.itemInfo.lineTo(0,heightStack);
   _parent.itemInfo.lineTo(0,31);
   _parent.itemInfo.endFill();

   color = new Number(0xf7f7f7);
   drawText(_item._description,10,heightStack,190,20,color,false,depth);

   _parent.itemInfo.beginFill(0x000000,25);
   _parent.itemInfo.moveTo(0,heightStack + 1);
   _parent.itemInfo.moveTo(200,heightStack + 1);
   _parent.itemInfo.lineTo(200,heightStack + _parent.itemInfo["itemName" + depth].maxscroll * 15 + 3);
   _parent.itemInfo.lineTo(0,heightStack + _parent.itemInfo["itemName" + depth].maxscroll * 15 + 3);
   _parent.itemInfo.lineTo(0,heightStack + 1);
   _parent.itemInfo.endFill();
}
function drawText(_text:String, xpos:Number, ypos:Number, xsize:Number, ysize:Number, color:Number, textShadow:Boolean, depth:Number)
{
   _parent.itemInfo.createTextField("itemNameShadow" + depth,depth,xpos,ypos + 2,xsize,ysize);
   _parent.itemInfo.createTextField("itemName" + depth,depth + 1,xpos,ypos,xsize,ysize);

   _parent.itemInfo["itemName" + depth].selectable = false;
   _parent.itemInfo["itemName" + depth].multiline = true;
   _parent.itemInfo["itemName" + depth].wordWrap = true;
   _parent.itemInfo["itemName" + depth].autoSize = true;

   _parent.itemInfo["itemName" + depth].text = _text;
   _parent.itemInfo["itemName" + depth].setTextFormat(dynamicText);
   _parent.itemInfo["itemName" + depth].textColor = color;
   if (textShadow)
   {
      _parent.itemInfo["itemNameShadow" + depth].selectable = false;
      _parent.itemInfo["itemNameShadow" + depth].multiline = true;
      _parent.itemInfo["itemNameShadow" + depth].wordWrap = true;
      _parent.itemInfo["itemNameShadow" + depth].autoSize = true;

      _parent.itemInfo["itemNameShadow" + depth].text = _text;
      _parent.itemInfo["itemNameShadow" + depth].setTextFormat(dynamicText);
      _parent.itemInfo["itemNameShadow" + depth].textColor = 0x000000;
   }
}

function drawItem(_item:Object)
{
   switch (_item._id)
   {
      case 1 :
         {
            _item.gotoAndStop("ring1");
            break;
         };
      case 2 :
         {
            _item.gotoAndStop("ring2");
            break;
         };
      case 3 :
         {
            _item.gotoAndStop("blade5");
            break;
         };
      case 4 :
         {
            _item.gotoAndStop("blade1");
            break;
         };
      case 5 :
         {
            _item.gotoAndStop("cards1");
            break;
         };
      case 6 :
         {
            _item.gotoAndStop("neckless1");
            break;
         };
      case 7 :
         {
            _item.gotoAndStop("earrings1");
            break;
         };
      case 50 :
         {
            _item.gotoAndStop("slime");
            break;
         };
      case 51 :
         {
            _item.gotoAndStop("greenSlime");
            break;
         };
      default :
         {
            _item.gotoAndStop(1);
      }
   }
};

Author:  Sadou [ Sun Mar 24, 2013 1:17 am ]
Post subject:  Re: My old inventory system (AS2)

That's a lot of code and its good you realize that it's a bit too complex. There are simpler ways of doing it. :D... Improvement is always great.

Author:  Odin Anarki [ Sun Mar 24, 2013 1:48 am ]
Post subject:  Re: My old inventory system (AS2)

Meh, Its medium sized

Author:  deadlyfox [ Sun Mar 24, 2013 2:05 am ]
Post subject:  Re: My old inventory system (AS2)

i have no fuck idea what it is so at least you know what it is

Page 1 of 1 Time zone: Pacific/Galapagos
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/