A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: My little inventory thingy.

  1. #1
    20x6 ziplockkitty's Avatar
    Join Date
    Dec 2001
    Location
    Connecticut!!!
    Posts
    697
    I was trying to set up a little inventory for my site but things didnt work to well. I used an array called items and a for loop checked to see if the item had been taken or not. Well, this was my first time using a for loop so things got a bit messy. This is what my code looks like:
    Code:
        if (hitTest(_root.hero) and Key.isDown(Key.SPACE)) {
            _root.text._visible = 1;
            for (i=0; i<=_root.text.itemcount; i++) {
                if (_root.text.items[i] !== "water") {
                    _root.text.text = "Water was added to inventory";
                    _root.text.items[_root.text.itemcount] = "water";
                    _root.text.itemcount++;
                } else if (_root.text.items[i] == "water") {
                    _root.text.text = "Nothing here";
                }
            }
        }
    Everytime i run this code flash freezes for a couple seconds, i must be doing something wrong. Thanks for any help.

  2. #2
    Senior Member Olorin's Avatar
    Join Date
    Aug 2000
    Posts
    1,151
    you're increasing the itemcount inside the loop, which makes it an infinite loop. (i and itemcount both increase by 1 every loop, so i never becomes greater than itemcount)

    And what exactly are you trying to do with this code ? it looks like you're adding a "water" to your inventory for every item you're carrying that's not "water".

    Olorin

  3. #3
    20x6 ziplockkitty's Avatar
    Join Date
    Dec 2001
    Location
    Connecticut!!!
    Posts
    697
    Ya, my code is a bit messed up. How would i check to see if any member of the array is "water"?

  4. #4
    20x6 ziplockkitty's Avatar
    Join Date
    Dec 2001
    Location
    Connecticut!!!
    Posts
    697
    Nevermind i got something working. I changed it to this if any one cares.
    Code:
    onClipEvent (load) {
        setting = false;
        _visible = 0;
    }
    onClipEvent (enterFrame) {
        if (hitTest(_root.hero) and Key.isDown(Key.SPACE)) {
            _root.text._visible = 1;
            for (i=0; i<=_root.text.itemcount; i++) {
                if (_root.text.items[i] == "water") {
                    setting = true;
                }
            }
            if (setting) {
                _root.text.text = "Nothing Here";
            }
            if (!setting) {
                _root.text.text = "Water was added to your inventory";
                _root.text.items[_root.text.itemcount] = "water";
                _root.text.itemcount++;
            }
        }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center