Skip to main content
Inspiring
September 24, 2015
Answered

Re-using inventory slots

  • September 24, 2015
  • 1 reply
  • 1171 views

Hi all, been building a point and click game and found a problem with my inventory system which is

when an item is added to the inventory and then used, another item will not re-use it's slot.

Code:

Place an instane of InvMC on the stage:

this.attachMovie('inventoryMC', 'invMC', depth);

setProperty("invMC", _x, "30.65");

setProperty("invMC", _y, "665");

within the invMC declare an array to handle 20 slots:

aryInv = new Array(19);

for(i=0;i<20;i++){

  aryInv="none";

}

Place 20 instances of "slotMC" within the inventory MC, number them "inv1", "inv." etc. Then

on each one put this code changing t=0 to t=1, t=2 etc:

onClipEvent (enterFrame) {

  t = 0;

  if (_root.invMC.aryInv == "none") {

  gotoAndStop(1);

  }

  if (_root.invMC.aryInv == "key1") {

  gotoAndStop(2);

  }

  if (_root.invMC.aryInv == "key2") {

  gotoAndStop(3);

  }

  if (_root.invMC.aryInv == "key3") {

  gotoAndStop(4);

  }

  if (_root.invMC.aryInv == "rat") {

  gotoAndStop(5);

  }

  if (_root.invMC.aryInv == "bell") {

  gotoAndStop(6);

  }

  if (_root.invMC.aryInv == "book") {

  gotoAndStop(7);

  }

  if (_root.invMC.aryInv == "candle") {

  gotoAndStop(8);

  }

  if (_root.invMC.aryInv == "sword") {

  gotoAndStop(9);

  }

  if (_root.invMC.aryInv == "match") {

  gotoAndStop(10);

  }

  if (_root.invMC.aryInv == "pipe") {

  gotoAndStop(11);

  }

  if (_root.invMC.aryInv == "stone key") {

  gotoAndStop(12);

  }

  if (_root.invMC.aryInv == "skull") {

  gotoAndStop(13);

  }

}

Put 20 frames into "slotMC" with an MC of each item on the frames. Put this code on the item MCs:

(slightly different targets etc for each item).

on (release) {

  if (_global.key1vis == 1) {

  startDrag("", true);

  _global.key1vis = 2;

  } else if (this.hitTest(Object(this._parent).sqReturnTarget)) {

  stopDrag();

  setProperty(this, _x, 15.95);

  setProperty(this, _y, 16.45);

  _global.key1vis = 1;

  } else if (this.hitTest(Object(_root).gateKeyTarget)) {

  this.unloadMovie();

  _global.key1vis = 2;

  setProperty(_root.gateKeyTarget, _visible, 0);

  _root.gotoAndStop("gates open");

  _global.castleGatesOpen = 2;

  }

  }

This works great - except each item takes the next numerical slot even if the previous one has been "vacated" by the item

the user has got rid of - so what do I need to do allow items to use up acted "slots" ?

All and any advice much appreciated - files available if it will help !

Best wishes

Tony

This topic has been closed for replies.
Correct answer Tony Heagren

sorry to hear you're not feeling well.

to display icons at the bottom of the screen, if your movieclip icon names are the same as your inventory strings and they have top-left reg point, you can use:

function displayF():Void{

var w:Number=0;

for(var i:Number=0;i<inventoryA.length;i++){

w+=this[inventoryA]._width;

}

var gap:Number = (Stage.width-w)/(inventoryA.length+1);

var nextX:Number = gap;

for(var i:Number=0;i<inventoryA.length;i++){

this[inventoryA]._x = nextX;

this[inventoryA]._y = Stage.height-this[inventoryA]._height;

nextX += gap+this[inventoryA]._width;

}

}


So morphine, pure oxygen and Rivoroxabin are playing games with my head (pleasantly on the whole) but it's impossible to concentrate on anything for long so

I did what computers do when it's all to much - I defaulted to 1970 and just thought about if/else

It works. It's old skool and the lovers of AS3 would say "clumsy" and "clunky" but.... it works faultlessly and it's easy to manipulate across the game.

So to pick up an item you find, click on it (it's a button) and on the button it says:

on (release) {

  if (slot1._currentframe == 1) {

  slot1.gotoAndStop("sword");

  } else if (slot2._currentframe == 1) {

  slot2.gotoAndStop("sword");

  } else if (slot3._currentframe == 1) {

  slot3.gotoAndStop("sword");

  } else if (slot4._currentframe == 1) {

  slot4.gotoAndStop("sword");

  } else if (slot5._currentframe == 1) {

  slot5.gotoAndStop("sword");

  } else if (slot6._currentframe == 1) {

  slot6.gotoAndStop("sword");

  } else if (slot7._currentframe == 1) {

  slot7.gotoAndStop("sword");

  } else if (slot8._currentframe == 1) {

  slot8.gotoAndStop("sword");

  }

}

[[ if anyone wants to write me a loop to do this more elegantly I'd be grateful because while I have the brain power to know it should

be a loop, I don't have enough to pull it off  : )  ]]

Set a few global variables to make things visible and then gone once they're used and we're away...

To use an item, try it out on a "target" which may or may not work and to return it to the inventory if it doesn't, the MC version of

the item (same picture) sits inside a "slotMC" on the main timeline (slot1, slot2 etc., which each have a frame to represent each item) have this

code on them so they behave like buttons:

on (release) {

  if (_global.swordVis == 1) {

  startDrag("");

  _global.swordVis = 2;

  } else if (this.hitTest(Object(this._parent).returnMC)) {

  stopDrag();

  setProperty(this, _x, 0.5);

  setProperty(this, _y, -3.75);

  _global.swordVis = 1;

  } else if (this.hitTest(Object(_root).killSwordMC)) {

  _global.swordVis = 2;

  setProperty(Object(_root).killSwordMC, _visible, 0);

  this._parent.gotoAndStop(1);

  }

}

No arrays, no functions and once the first lot is reduced to a loop thats not much code.

The "empty" test version of this which includes nothing but the basic elements to make it work rolls in as an .swf at 4k

which in my day, before lightning broadband everywhere, was important.

So I'm off to go gaming with my crazy new morphione fuelled game in a castle - when it's done I'll post a link here so all and sundry

can try to break it  : )

Best wishes

(and never smoke cigarettes or you'll end up full of tubes in a hospital feeling very confused and frustrated about stuff you used to know backwards)

Tony

1 reply

kglad
Community Expert
Community Expert
September 24, 2015

that's not a good inventory system.  you shouldn't be filling empty slots with 'none'.

you should just use slots that are available and use the splice method of arrays to remove items:

function inventoryRemoveF(itemS:String):Void{

_root.invMC.aryInv.splice(_root.invMC.aryInv.indexOf(itemS),1);

}

but if you want to continue using that, to reset a slot:

_root.invMC.aryInv[_root.invMC.aryInv.indexOf(itemS)]='none';

Inspiring
September 24, 2015

Thanks for the quick reply - looks like exactly what I'm after butI'm not quite at your level of expertise -

I've put the function on the main (_root) timeline and I've tried to invoke it when items are used (i.e. the hitTest) but

no joy. Could you explain how you would use it in a little more detail?

Thanks

Inspiring
September 24, 2015

Agreed - it's not a good inventory system !

I've cobbled it together over a few days using amended bits and bobs from all over - if you have any links

to a decent "end to end" tutorial I'd be very grateful.

Best wishes

Tony