Reply to topic  [ 4 posts ] 
 My old inventory system (AS2) 
Author Message
Level 38
Level 38
User avatar

Cash on hand:
-45,425.95

Bank:
2,750,364.30
Posts: 10362
Joined: Sun Oct 26, 2008 5:47 am
Group: Dev Team
Post 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);
      }
   }
};

_________________
My Pixiv
Image
Spoiler: show
OLD VERSION, BITCHES!
Image


Fri Mar 22, 2013 12:34 pm
Profile E-mail
Level 0
Level 0
User avatar

Cash on hand:
755.00
Posts: 19
Joined: Sat Mar 23, 2013 11:52 pm
Group: Registered users
Post 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.


Sun Mar 24, 2013 1:17 am
Profile E-mail
ℱᒪ૪ᓰﬡᘐ ᖘ⋒ᖇᖰᒪᙓ ᖘᙓﬡᓮᔕ
ℱᒪ૪ᓰﬡᘐ ᖘ⋒ᖇᖰᒪᙓ ᖘᙓﬡᓮᔕ
User avatar

Cash on hand:
333,340,195.50

Bank:
0.00
Posts: 18595
Joined: Fri Mar 04, 2011 9:57 pm
Location: ЇИ УОЦЯ MЇЙD FЦCКЇЙG ЇT ЇЙTО ОBLЇVЇОЙ
Group: Їи$aиїту
Country: Nepal (np)
Post Re: My old inventory system (AS2)
Meh, Its medium sized

_________________
PUNCH YO FACE
Apply To The Їи$aиїту Group! | Veiw My Ultimate Fh Tribute!
! ! '
© 2010 -2099 Headfuck The Wise. All Rights Reserved.

Quotes
Spoiler: show
Image
who149 wrote:
I'm trying i'm trying~ i'm making I'll try too slowly up my posting. At least once a day for a bit. Then I'll up that too twice, then four, then 8 and so on.

Until eventually I wake up one morning and find out that I am actually an Idiot hero.
On some quest too cheat on his gf or raise affection of 5 women who conveniently live in my the same dorm as me.
In which I only have 100 days to seduce them all.

Remon wrote:
Now we can dominate the porn industry, camera industry, AND the world!
YomToxic wrote:
YOU BETTER STAY ALIVE OR ELSE I WILL HUNT YOU DOWN AND RAPE YOU DEAD.

_________________
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
2 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.


Sun Mar 24, 2013 1:48 am
Profile E-mail WWW
Level 11
Level 11
User avatar

Cash on hand:
29,198.15
Posts: 1170
Joined: Sat Feb 23, 2013 10:24 pm
Group: Special Access
Country: United States (us)
Post Re: My old inventory system (AS2)
i have no fuck idea what it is so at least you know what it is

_________________
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.


Sun Mar 24, 2013 2:05 am
Profile E-mail
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 
 

Similar topics

 
my digestive system hates me
Forum: ./General Spam
Author: cluelessfurball
Replies: 39
I broke the FH$ system.
Forum: ./General Spam
Author: fluffy
Replies: 23
Team Forkheads Gaming System
Forum: ./Video Games
Author: ~cxc~
Replies: 17
essentially new search system
Forum: ./General Spam
Author: F_JUNK_DUMP
Replies: 2
The Family Political System
Forum: ./General No Spam
Author: Haz
Replies: 3
Top


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Mods Database :: Imprint :: Crawler Feeds :: Reset blocks
Designed by STSoftware for PTF.

Portal XL 5.0 ~ Premod 0.3 phpBB SEO