Flash Actionscript 2.0 – Making a Treasure Chest

This tutorial will show you how to make objects that hold items or money or whatever you want to put in them for your characters to pick up when they’re opened. I’m making a treasure chest but you can do whatever you want whether it be a chest, a box, a coffin, or even a clam. If you don’t already have a character made that can move around, you can find a link to a tutorial on how to make one in the resources section on this page.

Step 1 – Setting up the Chest

To start off, make your treasure chest and then click on it, press f8, and set type to movie clip. You can name it whatever you like but it’s always best to keep your objects’ names relevant so they’ll be easy to spot when you’re in the middle of a huge project. Once that’s all done and you’ve clicked OK, double click on your chest to bring up its personal editing screen. You’ll know it’s on there when you see your chest’s name in the top left area next to where it says “Scene 1.”

While on this screen, press f6 to add a second frame and on this frame design your chest to look like it’s open. Now click on your first frame, press f9, and in the actions window type: stop(); Without this command your chest will keep cycling through both frames when you test and I’m sure none of us want a chest that look’s like it’s talking to you. Or maybe you do. I don’t know but if you do there you go. :) Anyways, Now you can go back to the main screen so press the back arrow next to where it says “Scene 1.”

Step 2 – Setting up the Variables

Click on your main frame, press f9, and in the action screen type in:

Gold = 0;
ClosedChest = true;
found50gold._alpha = 0;

You have your gold, or cash or money or yen, whatever you want to call it, starting off at 0, You’re ClosedChest variable which is saying your chest is closed, and you’re found50gold message which has it’s alpha set to 0 so it’s invisible.

Step 3 – Coding

Now for the part I’m sure you’re mainly here for. The coding for the chest. Click on your chest, press f9, and in the action window type:

onClipEvent(enterFrame){
if (this.hitTest (_root.Box) and _root.ClosedChest==true and (Key.isDown(Key.SPACE))){
this.gotoAndStop(2);
_root.Gold+=50;
_root.ClosedChest = false;
_root.found50gold._alpha = 100;
}
}

First of all, where it says _root.Box when I made my character in my “Making Your Character Move” tutorial I named it Box. So when you type that part just replace Box with whatever you called your character. So with that out the way I’ll explain what’s going on here.

You made a hitTest so your character has to be touching the box and the box has to be closed in order for you to press SPACE to open it. Once the box is opened, the next lines are saying it will add 50 gold to your character’s gold variable, The ClosedChest variable will be set to false so people can’t keep reopening it adding more gold every time, and your found50gold message’s alpha will become 100 so it’s visible and people can see what they just got.

To make your found50gold message just make a static text above your chest saying: Found 50 Gold! and press f8 to set it to a movie clip, then press OK and on the top right in it’s properties under instance name type in found50gold. The instance name will link the message to the variable to tell it to go invisible.

Last thing to do is make another static text somewhere like in the top corner saying “Gold -” and next to it make a dynamic text which should be left blank. In the dynamic text’s properties, where it says variable, type in Gold so that it will be linked to the Gold variable and show a number to indicate how much gold your character has. When they open the chest, the number should go up by what you set it in the Chest’s coding.

With all that done you can press CTRL+Enter to test your project and make sure everything goes well. If you’re confused about any parts or have any kind of questions you can e-mail me at [email protected]. Take care and good luck.


People also view

Leave a Reply

Your email address will not be published. Required fields are marked *