Step 10. Using Items
This tutorial is not going to go over how to drag items and combine them; it will instead be a simple click to use.
Inside the room where you want the item to be used, give the item box an event listener.
Object(root).game.Inventory.Invbox3.addEventListener(MouseEvent.CLICK, useInvbox3); function useInvbox3(event: MouseEvent): void { }
Do whatever action you want it to do. In this case, when using the cheese, it makes Billy (the main character) go to the 2nd frame and the textbox sez stuff.
Object(root).game.Inventory.Invbox3.addEventListener(MouseEvent.CLICK, useInvbox3); function useInvbox3(event: MouseEvent): void { this.billymc.gotoAndStop(2); this.textboxmc.gotoAndStop("usecheez"); Object(root).game.Inventory.Invbox3.gotoAndStop("empty"); }
We also have to make sure to only do this function if you have the correct item in the slot.
Object(root).game.Inventory.Invbox3.addEventListener(MouseEvent.CLICK, useInvbox3); function useInvbox3(event: MouseEvent): void { if (Object(root).game.Inventory.Invbox3.currentLabel == "cheez") { this.billymc.gotoAndStop(2); this.textboxmc.gotoAndStop("usecheez"); Object(root).game.Inventory.Invbox3.gotoAndStop("empty"); } }
Alternatively, if you don’t wanna use labels, you can compare the item box’s currentFrame if you want to use numbers.
You may notice that if you pick up an item and then return it, the item comes back. We need to use variables to keep track of if the item has been picked up or not.
In AS3, when variables are defined, they are defined in this format
var NAME: TYPE = VALUE;
When they are called, they have this format:
NAME = VALUE;
There are 4 main types of variables you should know about. Strings, Numbers, ints, and Booleans.
Strings - Used to store words and are enclosed using quotes
var testString: String = “This is a string!”;
Numbers - Used to store… numbers. It also supports decimals, NAN, and infinity (if you need those for whatever reason)
var testNumber: Number = 3.14;
Integers - Used to store whole numbers and have a range between -2,147,483,648 and 2,147,483,647 (If you wanna be cool, call it a signed 32-bit integer)
var testInt: int = 54;
Booleans - Can have two values, true and false.
var testBoolean: Boolean = false;
In the root, define a boolean variable (unless there are multiple states in an object, in which case you should use an int)
var hasCheese: Boolean = false;
Now, back to the room with the item, make it so this is true when you pick up the item. You need to add a MovieClip prefix to the start of the variable and inside the parentheses, the location of the variable.
this.cheezmc.addEventListener(MouseEvent.CLICK, cheezget); function cheezget(event: MouseEvent): void { MovieClip(root).hasCheese = true; this.textboxmc.gotoAndStop("cheez"); this.cheezmc.visible = false; Object(root).game.Inventory.Invbox3.Itemboxflash.gotoAndPlay(1); Object(root).game.Inventory.Invbox3.gotoAndStop("cheez"); }
Now we need to add an if check to see if it should show the item or not. You use == to compare two values. Make sure to know the diffrence
- == - used to compare two values
- = - used to set a value
if (MovieClip(root).hasCheese == true) { this.cheezmc.visible = false; } else { this.cheezmc.visible = true; }
What this is doing is comparing the hasCheese variable located in the root to true. If they are the same, both being true, that means that the play has taken the item and shouldn’t be showing it. If they don’t equal, then item hasn’t been taken so it should be shown.