-
20x6
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.
-
Senior Member
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
-
20x6
Ya, my code is a bit messed up. How would i check to see if any member of the array is "water"?
-
20x6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|