# Inventory Check ---\> checking for certain items

**URL:** <https://forum.kirupa.com/t/inventory-check-checking-for-certain-items/102070>\
**Category:** Uncategorized\
**Created:** [February 19, 2004, 12:41pm UTC](https://forum.kirupa.com/t/inventory-check-checking-for-certain-items/102070 "2004-02-19T12:41:41Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fargate](https://avatars.discourse-cdn.com/v4/letter/f/dbc845/32.png) [@Fargate](https://forum.kirupa.com/u/Fargate)\
**Post date:** [February 19, 2004, 12:41pm UTC](https://forum.kirupa.com/t/inventory-check-checking-for-certain-items/102070/1 "2004-02-19T12:41:41Z")

</div>

Okay I have the following Inventory code.

This adds it your inventory slots

[AS]  
currentslotnum = 1;  
stop();  
function addToslot(item) {  
if (!item.found) {  
item.\_x = eval(“itemSlot”+currentslotnum).\_x;  
item.\_y = eval(“itemSlot”+currentslotnum).\_y;  
item.found = true;  
currentslotnum++;  
}  
}  
[/AS]

so that’s good…

And this allows you to pick it up

[AS]  
onClipEvent (enterFrame) {  
if (\_root.character.hitTest(this)) {  
\_root.addToslot(this);  
}  
}  
[/AS]

Great so that all works perfectly. But Say If want to check if a user has a certain item if so…such an such happens…etc. But I can’t figure out how I would do this with an If statement. No matter what I can’t get it to work( That is what you’d use right?). Can anyone tell me the best way to do a “check” for this type of thing. Thanks in advance :).

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 19, 2004, 1:44pm UTC](https://forum.kirupa.com/t/inventory-check-checking-for-certain-items/102070/2 "2004-02-19T13:44:00Z")

</div>

Maybe push the items into an array and then use a for loop inside the array if you want to check for any items.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 20, 2004, 1:32am UTC](https://forum.kirupa.com/t/inventory-check-checking-for-certain-items/102070/3 "2004-02-20T01:32:28Z")

</div>

How would I do that? I mean how could I use an array to check whether the object has been picked up yet or not? I don’t see how :h:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 20, 2004, 3:02am UTC](https://forum.kirupa.com/t/inventory-check-checking-for-certain-items/102070/4 "2004-02-20T03:02:54Z")

</div>

WHen the character hittest the item, push the item to the array.  
Then later, run a for loop inside the array to check if some item exists.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 20, 2004, 3:06am UTC](https://forum.kirupa.com/t/inventory-check-checking-for-certain-items/102070/5 "2004-02-20T03:06:02Z")

</div>

Err…I’m very confused…could you give me an example maybe?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 20, 2004, 6:41am UTC](https://forum.kirupa.com/t/inventory-check-checking-for-certain-items/102070/6 "2004-02-20T06:41:17Z")

</div>

For example:

```auto
inventory = new Array();//inventory array to store the items collected
Array.prototype.find = function(item) {//prototype to search for a string inside an array
	var found = false;
	for (var i = 0; i<this.length; i++) {
		if (this* == item) {
			found = true;
		}
	}
	return found;
};
function addToslot(item) {//function to add item to the inventory
	if (!inventory.find(item)) {//if the item isnt already in the inventory
		inventory.push(item);//add the item to the inventory
	}
}
stop();

```

And on your item:

```auto
onClipEvent (enterFrame) {
        if (_root.character.hitTest(this)) {
                _root.addToslot(this);
        }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 21, 2004, 3:58am UTC](https://forum.kirupa.com/t/inventory-check-checking-for-certain-items/102070/7 "2004-02-21T03:58:03Z")

</div>

Okay…but how is that any different than my script…I mean it just allows me to add stuff to my inventory…With the above I can’t check if I have specific individual items, and in turn get flash to execute certain things can I? Perhaps you could give me an example .fla because I don’t see how that allows you to check stuff :h:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 21, 2004, 4:03am UTC](https://forum.kirupa.com/t/inventory-check-checking-for-certain-items/102070/8 "2004-02-21T04:03:30Z")

</div>

You can check if the item is in the inventory with

```auto
inventory.find(item);//return true or false

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 22, 2004, 1:49am UTC](https://forum.kirupa.com/t/inventory-check-checking-for-certain-items/102070/9 "2004-02-22T01:49:52Z")

</div>

Alright, if the character thing touched any item that’s collectable, he obviously has it because in your code, any item touched goes to the inventory. So in your main code, add another function like so:

```auto
function itemCheck () {
// The bottle is an example item:
if (_root.character.hitTest (_root.bottle)) {
   // Do Something Here:
}
if (_root.character.hitTest (_root.car)) {
  // Do Something Here:
}
}
setInterval (itemCheck, 0);

```

- So basically, this code keeps checking if certain items are touched, and if they are, an action is performed (which you have to assign).

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 22, 2004, 4:29am UTC](https://forum.kirupa.com/t/inventory-check-checking-for-certain-items/102070/10 "2004-02-22T04:29:10Z")

</div>

but as far as I can understand your code claudio…If you don’t have the item when the script checks for it, it will push it into your inventory anyways? How would that work???

As for you suggestion, Sharif I understand how that would work but that wouldn’t help you actually have the ability to say if you have a certain item in your inventory you are then allowed to perform another action.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 22, 2004, 4:31am UTC](https://forum.kirupa.com/t/inventory-check-checking-for-certain-items/102070/11 "2004-02-22T04:31:24Z")

</div>

Then you are not understanding the code.  
_If the character hittests the item, you perform the checking. If the item isnt already in the inventory, then add it._

[edit]ops, the enterframe should go on the item, not the character[/edit]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 22, 2004, 5:16am UTC](https://forum.kirupa.com/t/inventory-check-checking-for-certain-items/102070/12 "2004-02-22T05:16:46Z")

</div>

Something like this.
