9 Replies Latest reply on Oct 1, 2015 3:10 PM by Tony Heagren

    Re-using inventory slots

    Tony Heagren

      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.




      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);





      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[t] == "none") {



        if (_root.invMC.aryInv[t] == "key1") {



        if (_root.invMC.aryInv[t] == "key2") {



        if (_root.invMC.aryInv[t] == "key3") {



        if (_root.invMC.aryInv[t] == "rat") {



        if (_root.invMC.aryInv[t] == "bell") {



        if (_root.invMC.aryInv[t] == "book") {



        if (_root.invMC.aryInv[t] == "candle") {



        if (_root.invMC.aryInv[t] == "sword") {



        if (_root.invMC.aryInv[t] == "match") {



        if (_root.invMC.aryInv[t] == "pipe") {



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



        if (_root.invMC.aryInv[t] == "skull") {





      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)) {


        setProperty(this, _x, 15.95);

        setProperty(this, _y, 16.45);

        _global.key1vis = 1;

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


        _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


        • 1. Re: Re-using inventory slots
          kglad Adobe Community Professional & MVP

          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{




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



          • 2. Re: Re-using inventory slots
            Tony Heagren Level 1

            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?



            • 3. Re: Re-using inventory slots
              Tony Heagren Level 1

              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


              • 4. Re: Re-using inventory slots
                kglad Adobe Community Professional & MVP

                all you need for an inventory system is to add and remove items and check if they're present:


                var inventoryA:Array = [];

                var maxInventoryNum:Number = 20;


                function addToInventoryF(itemS:String):Void{



                    } else {




                function removeFromInventoryF(itemS:String):Void{



                function checkInventoryF(itemS:String):Boolean{


                        return true;

                    } else {

                        return false;



                function errorF():Void{

                    // do whatever.  item cannot be added


                • 5. Re: Re-using inventory slots
                  Tony Heagren Level 1

                  Thanks - that looks like a really elegant set of functions


                  Trouble is I'm nine parts designer and 1 part coder - I don't see how the functions refer to any slots

                  in a visual inventory MC on the stage or how I would call the functions from "on (release)" of an item - I want to

                  work out as much of this as I can myself but I simply don't know how to link the visual stuff on the stage

                  to this code.


                  Any help much appreciated

                  • 6. Re: Re-using inventory slots
                    kglad Adobe Community Professional & MVP

                    you should never attach code to objects.  so all your on(whatever) code is poor coding.  the sooner you stop doing that, the better.


                    but if those functions are placed on your main timeline, you can reference them from anywhere using:


                    _root.removeFromInventory('pipe');  // for example.



                    any time you wanted to display the inventory items in a list component, for example, call listF()


                    var list: List = new List();

                    list.setSize(300, 80);

                    var dp: DataProvider = new DataProvider();

                    list.dataProvider = dp;


                    function listF():Void{


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




                    • 7. Re: Re-using inventory slots
                      Tony Heagren Level 1

                      But i like putting the code on the buttons !


                      Like all designers and creatives I just like the idea that the button does what it says "ON THE BUTTON" !

                      Having to write "event listener_button blah blah" is for the programmers and, in my view, it's what killed off FLash

                      as a commercial and creative way forward on the web. It's why Apple was able to say "no more" on the iPad - because it was so slow !

                      Back in the days of AS2 things worked fast because although the net itself was slower, the flash player was dealing with less complex stuff,

                      the code was on the button and the button did as it was told - art students and graphics people got into flash easily because it made simple sense.

                      They made flash cool by making it look great and do cool things - as soon as you had to learn AS3 with it's events and listeners everyone was turned off.


                      Anyway - I digress...


                      I can't make the functions you gave me work with the stuff I have on stage and I don't want to list the contents of an Array in a list component.

                      I just want a group of icons at the bottom of the screen like any point-and-click game. You find an item, click it, it goes into the inventory.

                      When you use it (drag then hitTest) it disappears and that slot becomes vacant for the next thing you might find. Easy I'm sure with the functions you gave me

                      but only if you have the level of understanding to implement it.


                      Earlier you said "all you need for an inventory system is to add and remove items and check if they're present" and in response I could say

                      "all you need to put up a skyscraper is some plans and a few builders". It's true - but it's hardly the whole story and, as a lot of ugly cities pay testament to, it's easy to do badly...


                      I'm in hospital recovering from surgery (and on some pretty heavyweight drugs so if I'm incoherent or rude I apologise enormously), I'm trying to keep

                      my mind working by building a pretty simple game to show my kids, if you can offer some practical help in terms of actually building an inventory system

                      with buttons and icons the way I've described it (a la Daymare town) that would be great. I really appreciate you taking the time mate.


                      Best wishes


                      • 8. Re: Re-using inventory slots
                        kglad Adobe Community Professional & MVP

                        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++){



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

                        var nextX:Number = gap;

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

                        this[inventoryA[i]]._x = nextX;

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

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



                        • 9. Re: Re-using inventory slots
                          Tony Heagren Level 1

                          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) {


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


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


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


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


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


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


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






                          [[ 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) {


                            _global.swordVis = 2;

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


                            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);





                          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)