Vectorials
Flash Perfection
3D Lessons
Tutorialkit
Markup Tutorials
Learn PHP
network
adv banner
Flash & Swish  Home Flash & Swish Flash Tutorials Intro to Variables
rss

Intro to Variables

Author: Stian Bakkane More by this author


You probably ask yourself, what is a variable? In this tutorial I will use easy - to - understand terms to make it a bit clearer for you what this actually is about.

I will start by saying that a variable is like a box. And you can put whatever you want in that box. And then you can take the content out of the box and replace it with something else.

But why? Why would you want to use variables in a flash animation? Well, let's say you want to make a game. And the point of the game is to get out of a room. The only problem is that the door is locked. But the key is somewhere in the room.

To make this game work, you need to know what variables are and how to use them.

Let's say that you have a scene in Flash. And in that scene you have placed a door (button) and a desk (button). What you have to do now is to make sure that the player can only open the door if he has picked up the key from the desk.

Create a new layer and call it "var_define". This will be our action layer and will control the stop( ); functions as well as the first definition of the variable we will use to determine whether the player has the key or not.

image 1

Press F9 to open the actions panel. Remember that frame 1 in the "var_define" layer has to be active. In the actions panel write exactly the same as in the image below. Don't worry. I'll explain later.

image 2

You're probably wonder what the heck is going on. But I'll explain it to you. Remember we talked about the variables as buckets? Well it still is, but this time it's more like the player's pocket. the line "var key = 0" means that the "key" is the variable. And by using the "var" command, we can define the value of the "key" variable.

So basically what we want to do here is to set the "key" variable to 0. This is the same as saying that the player doesn't have the key to the door. Remember that the name "key" can be whatever you want.

As you see, I've added a stop command to the first frame, but I'm not going to go deeper into that.

So now we have made sure that the player doesn't start with the key in his pocket.

But we are not finished yet! Select the door and go to the actions panel. Again, type exactly the same as I've done.

image 3

So.. what is this now? If you know the basics of actionscripting, you know that if you want a button to execute a command when you click it, you will have to use the "on (release) " command.

So that's exactly what I've done! But inside the command, there's a few confusing words.

Basically what I've done is to make the door ask the player if he's got the key or not. And that is done with an "if" command. Inside the two brackets is the variable we're using and it's value.

So if the player tries to enter the door with the "key" variable set to 1, the flash animation will go to frame 2.

But if you remember, we set the "key" variable to 0 in the first frame. This means that if he tries to open the door right away, he will just go to frame 1 again.

But how does he get the key? Select the desk and open the actions panel. Again, type exactly the same as I've done.

image 4

By now you should be familiar with the "var" command. So this shouldn't look too strange to you.

Now, when the player clicks on the desk the "key" variable will be set to 1.. This means that he will get the key and can open the door.

Now you can create a new layer and call it "text" or something. In that layer create a stop command and a static text field saying something like "You got out of the room!"

Congratulations! You just created a game using Macromedia Flash!



Author's URL: www.photoshopaid.net

Rate this Material: Bad 1 2 3 4 5 Excellent
print this page tell a friend subscribe to newsletter subscribe to rss

Add comments to "Intro to Variables"