Inventory System Problem

A place for tutorials on how to get the most out of Flash

Inventory System Problem

Postby bob12321 » Tue Apr 24, 2012 4:01 am

I have most of it complete except when I try to remove more than one circle it messes up. I'm not exactly sure whats causing my problem but I'm hoping someone here can figure it out.

Here is the source code:

Code: Select All Code
item1.visible = false;
item2.visible = false;
item3.visible = false;
item4.visible = false;

var currentslotnum:int = 1;
var numberStored:int = 1;
var item1x:int = 0;
var item1y:int = 0;
var item2x:int = 0;
var item2y:int = 0;
var item3x:int = 0;
var item3y:int = 0;
var item4x:int = 0;
var item4y:int = 0;

var slotBool:Boolean = false;

var newlyOpenSpot:int = 1;

item1Button.addEventListener(MouseEvent.CLICK, item1Buttonclick);
function item1Buttonclick(event:MouseEvent):void
{
   addToSlot(item1);
   
}

item2Button.addEventListener(MouseEvent.CLICK, item2Buttonclick);
function item2Buttonclick(event:MouseEvent):void
{
   addToSlot(item2);
}

item3Button.addEventListener(MouseEvent.CLICK, item3Buttonclick);
function item3Buttonclick(event:MouseEvent):void
{
   addToSlot(item3);
}

item4Button.addEventListener(MouseEvent.CLICK, item4Buttonclick);
function item4Buttonclick(event:MouseEvent):void
{
   addToSlot(item4);
}

function addToSlot(item)
{
   if(slotBool)
   {
      if(!item.found)
      {
         item.x = getChildByName("itemslot" + newlyOpenSpot).x;
         item.y = getChildByName("itemslot" + newlyOpenSpot).y;
         item.numberStored = newlyOpenSpot;
         if(item == item1)
         {
            item1x = item.x;
            item1y = item.y;
         }
         if(item == item2)
         {
            item2x = item.x;
            item2y = item.y;
         }
         if(item == item3)
         {
            item3x = item.x;
            item3y = item.y;
         }
         if(item == item4)
         {
            item4x = item.x;
            item4y = item.y;
         }
         item.found = true;
         slotBool = false;
      }
   }
   if(!slotBool)
   {
      if(!item.found)
      {
         item.x = getChildByName("itemslot" + currentslotnum).x;
         item.y = getChildByName("itemslot" + currentslotnum).y;
         item.numberStored = currentslotnum;
         if(item == item1)
         {
            item1x = item.x;
            item1y = item.y;
         }
         if(item == item2)
         {
            item2x = item.x;
            item2y = item.y;
         }
         if(item == item3)
         {
            item3x = item.x;
            item3y = item.y;
         }
         if(item == item4)
         {
            item4x = item.x;
            item4y = item.y;
         }
         item.found = true;
         currentslotnum++;
      }
   }
   item.visible = true;
}
var searcher:int = 0;
function removeSlot(item)
{
   if(item.found)
   {
      slotBool = true;
      item.found = false;
      if(item.numberStored == 1)
      {
         newlyOpenSpot = item.numberStored;
      }
      if(item.numberStored == 2)
      {
         newlyOpenSpot = item.numberStored;
      }
      if(item.numberStored == 3)
      {
         newlyOpenSpot = item.numberStored;
      }
      if(item.numberStored == 4)
      {
         newlyOpenSpot = item.numberStored;
      }
      if(item.numberStored == 5)
      {
         newlyOpenSpot = item.numberStored;
      }
      if(item.numberStored == 6)
      {
         newlyOpenSpot = item.numberStored;
      }
   }
}

item1.addEventListener(MouseEvent.MOUSE_DOWN, item1ToDrag);
function item1ToDrag(event:MouseEvent):void
{
   item1.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, item1ToDrop);
function item1ToDrop(event:MouseEvent):void
{
   item1.stopDrag();
   if(item1.hitTestObject(test1))
   {
      item1.x = test1.x;
      item1.y = test1.y;
      removeSlot(item1);
   }
   else
   {
      item1.x = item1x;
      item1.y = item1y;
   }
}

item2.addEventListener(MouseEvent.MOUSE_DOWN, item2ToDrag);
function item2ToDrag(event:MouseEvent):void
{
   item2.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, item2ToDrop);
function item2ToDrop(event:MouseEvent):void
{
   item2.stopDrag();
   if(item2.hitTestObject(test2))
   {
      item2.x = test2.x;
      item2.y = test2.y
      removeSlot(item2);
   }
   else
   {
      item2.x = item2x;
      item2.y = item2y;
   }
}

item3.addEventListener(MouseEvent.MOUSE_DOWN, item3ToDrag);
function item3ToDrag(event:MouseEvent):void
{
   item3.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, item3ToDrop);
function item3ToDrop(event:MouseEvent):void
{
   item3.stopDrag();
   if(item3.hitTestObject(test3))
   {
      item3.x = test3.x;
      item3.y = test3.y
      removeSlot(item3);
   }
   else
   {
      item3.x = item3x;
      item3.y = item3y;
   }
}

item4.addEventListener(MouseEvent.MOUSE_DOWN, item4ToDrag);
function item4ToDrag(event:MouseEvent):void
{
   item4.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, item4ToDrop);
function item4ToDrop(event:MouseEvent):void
{
   item4.stopDrag();
   if(item4.hitTestObject(test4))
   {
      item4.x = test4.x;
      item4.y = test4.y
      removeSlot(item4);
   }
   else
   {
      item4.x = item4x;
      item4.y = item4y;
   }
}


And attached is the swf for you to see my problem first hand.

Thanks in advance to anyone who can help.
Attachments
Click to Play
(Javascript Required)

inventoryProblem.swf [ 10.08 KiB | Viewed 2866 times ]

User avatar
bob12321
 
Joined: Sat Jul 23, 2011 5:12 pm

Re: Inventory System Problem

Postby bob12321 » Tue Apr 24, 2012 6:16 am

Actually I just figured out a solution for my problem and now have a working inventory. Since this is in the tutorial section if anyone wants find out how I did it then post here. If not then I'll probably just delete this thread.
User avatar
bob12321
 
Joined: Sat Jul 23, 2011 5:12 pm

Re: Inventory System Problem

Postby BlueLight » Tue Apr 24, 2012 2:34 pm

Question, how the hell was i able to move the Green and Blue circle to the lower boxes. It seem like they were set to them after i did it.
And why do you have a variable for each box? if you use something like a array or arraylist then it shouldn't matter where the item is added.
User avatar
BlueLight
Gangs n' Whores Developer
 
Joined: Sat Jun 04, 2011 8:23 am

Re: Inventory System Problem

Postby bob12321 » Tue Apr 24, 2012 3:08 pm

BlueLight Wrote:Question, how the hell was i able to move the Green and Blue circle to the lower boxes. It seem like they were set to them after i did it.
And why do you have a variable for each box? if you use something like a array or arraylist then it shouldn't matter where the item is added.


Well the reason why you can add it back to the inventory after taking it to the destination box is because when it is draged to the destination box, it is removed from the inventory. Since the item is no longer found, it can then be added again. Normally the button that adds something to the inventory will disappear and this will solve that problem. And the reason why I have a variable for each box is because I didn't want to use an array :)
User avatar
bob12321
 
Joined: Sat Jul 23, 2011 5:12 pm

Re: Inventory System Problem

Postby BlueLight » Tue Apr 24, 2012 9:45 pm

bob12321 Wrote:
BlueLight Wrote:Question, how the hell was i able to move the Green and Blue circle to the lower boxes. It seem like they were set to them after i did it.
And why do you have a variable for each box? if you use something like a array or arraylist then it shouldn't matter where the item is added.


Well the reason why you can add it back to the inventory after taking it to the destination box is because when it is draged to the destination box, it is removed from the inventory. Since the item is no longer found, it can then be added again. Normally the button that adds something to the inventory will disappear and this will solve that problem. And the reason why I have a variable for each box is because I didn't want to use an array :)


Lets say the slots are numbered

1,2
3,4
5,6

G = Green
R = Red
B = Blue
P = purple

normally it's
G,R
B,P
5,6

but i was able to get it
1,2
B,P
R,G

I believe and i have

R,2
3, (blue under purple)
G,6
User avatar
BlueLight
Gangs n' Whores Developer
 
Joined: Sat Jun 04, 2011 8:23 am

Re: Inventory System Problem

Postby bob12321 » Sun May 06, 2012 4:51 am

Like I said before this was a none working version, but here is a working version. If you can break it let me know how.
Also I decided to make it so the items aren't deleted after using them. For example in my game Escape! I currently have the items disappearing after use. Since a toilet plunger wouldn't disappear into thin air after using it
{ that would be problematic for anyone who needs to use one a lot :) }
Click to Play
(Javascript Required)

inventorySolved.swf [ 206.39 KiB | Viewed 2732 times ]

User avatar
bob12321
 
Joined: Sat Jul 23, 2011 5:12 pm

Re: Inventory System Problem

Postby hat973 » Tue Dec 31, 2013 12:11 am

im wonder how would u code something like this
Pockets.jpg
Pockets.jpg (110.11 KiB) Viewed 1595 times


i would like a as2 script if posable >.> iv been looking for tutorials nothing showing up im just talking about the inventory and money system
Red light panda

:ugeek: Tumbler: Tumbler
:geek: I have a Blog: Red Light Blog
;) I stream to Give me a Watch over at: Picarto
:mrgreen: Donate: Patreon
:roll: Discord:Discord
User avatar
hat973
 
Joined: Tue Apr 24, 2012 12:44 am

Re: Inventory System Problem

Postby BlueLight » Tue Dec 31, 2013 9:00 am

^^ this is basic in some ways if you think about it. The above code is a example of bad programming examples.
What i'd do is make a array since the number of items slots isn't going to change, then i'd make a algorithm to figure out which button you move to if you press up or down.

so basically create an array and method to fill it; remove item; set the picture to default; and set the picture to something related to the item. then all you need to do is make your GUI and have it get the information from the array. the way i would go about this is by giving each slot a unique number and then the slot would get the element based on it's number.

Make sense?
User avatar
BlueLight
Gangs n' Whores Developer
 
Joined: Sat Jun 04, 2011 8:23 am

Re: Inventory System Problem

Postby hat973 » Tue Dec 31, 2013 10:46 pm

so basicly id make one slot item slot 1 then item slot 2 and so on but every inventroy helper are for top veiw games what if say i want the space to be the action key to pick up go in doors and so on
Red light panda

:ugeek: Tumbler: Tumbler
:geek: I have a Blog: Red Light Blog
;) I stream to Give me a Watch over at: Picarto
:mrgreen: Donate: Patreon
:roll: Discord:Discord
User avatar
hat973
 
Joined: Tue Apr 24, 2012 12:44 am


Return to Tutorials



Who is online

Users browsing this forum: No registered users