The Armor Games website will be down for maintenance on Monday 10/7/2024
starting at 10:00 AM Pacific time. We apologize for the inconvenience.

ForumsProgramming ForumA Series of Mini-Lessons in Flash for the Absolute Beginner [AS3]

1 4410
ExplosionsHurt
offline
ExplosionsHurt
248 posts
Nomad

Disclaimer: This is REALLY for the absolute beginner. If you know anything about Flash, this probably isn't for you.

I am making a short 101 course in Adobe Flash CS5, step by step. No one can learn to code in a day (or even in a week!), but I'm planning to make a short series that will teach some things and hopefully get you started in making Flash stuff.

Each Mini-Lesson is exactly that: mini. I am intending to only cover one thing per post in this thread, so that it doesn't overwhelm.

The first lesson is going to be extremely simple and only cover basic variables. I will use AS3 for these, mainly because I can't code in AS2.

I'll do the variables mini-lesson in the next post!

  • 1 Reply
ExplosionsHurt
offline
ExplosionsHurt
248 posts
Nomad

What Are Variables

Think of variables as labelled jars that contain different types of data. There are many types of variables, but we'll only look at three for now:

String
Number
Boolean

A String is a series of letters, numbers, and some characters that are contained within quotation marks. ("&quot and ('' can both be used. An example of string might be "Hello! I'm learning variables in Flash today!" Strings are generally used for storing text.

A Number is exactly what you would think it is: a number. You don't store them in quotation marks like strings. Instead, number variables are written as they are. An example a number might be 9. Or 10. Or 32.432. Yes, you can do decimals too.

Lastly, a Boolean is always either true or false. It is sort of like an on/off switch, so you can use a boolean variable to ask true or false questions in your code. For example, if you were making Elephant Quest, you might need to ask "Does the player have the key?" and it was true, you would then open the door.

So how do I put a variable in my code?
A variable in AS3 (Actionscript 3) looks like this:

var myVariable:String = "Hello World!"


The variable has 4 parts in it. The first is the var at the front. This tells the computer that you are declaring a variable. The second is the name, which in my case is myVariable. All programming languages have something called reserve words, which means that they can't be used as variable names. In Flash, reserve words will generally change colour when you type them.

The third part is the :String. This tells the computer what you want to put in your variable, in this case a String. When you type this part, make sure to put a colon ( in front, and capitalize the word.

The last part is what is in the variable, in this case, the text "Hello World". Note that you do not have to do this and you can just type:

var ArmorGames:String;


This just makes an empty placeholder for the variable ArmorGames.

You'll also notice a semi-colon at the end of the thing. This tells the computer that you're done with this statement.

Make sure to remember your semicolons! They're very important!

Now to actually use a variable

Now you know how to make variables, but you can't do anything with it yet. Now we will use these variables in the trace() function.

Open up your copy (or trial) of Adobe Flash. For this, I will assume you're using CS4 or CS5.

Now, click on the ActionScript 3 button. You should now have a new window open in Flash. On the bottom (or top if you're using CS3) of the screen, there should be numbers, and a weird rectangle with a circle in it under the number 1. This is called the Timeline.

Right click on the first rectangle thingy (called a Frame). This will open up the right click menu. Down the bottom of the list, there should be a button that says "Actions". Click on it.

The box you see now is where you do the actionscripting. Each frame has it's own box, so you may have different code in different frames. For now, we'll be only using one frame.

Type (DO NOT COPY) the following code into the box:

var myVariable:Number = 5;


You do not need to call it myVariable, so feel free to call it what you like.

Let's have a look at other variable types we can declare. Type the following:

var myString:String = "Hello World!";

var myBoolean:Boolean = false;


Now you should have 3 variables declared. Now type the following:

trace(myString);


You'll notice we've added in the line trace(myString). This makes what you put in the brackets appear in the Output box. (Don't worry if you don't know what the Output box is yet.) The parenthesis(brackets) following the word trace allow you to provide data the computer might need to do whatever. In this case, we gave it myString, so the computer should output "Hello World!".

Your box should now look like:

http://tinypic.com/r/tarj1g/7

Now test your movie. Go to Debug --> Debug Movie --> In Flash Professional or just press Ctrl-Enter.

If you did it right, the words "Hello World!" should appear in the output box.

Try putting the other variables in the brackets and see what happens!

Still don't know what the Output box is? Click here.

Help! It didn't work!

This is the debugging section of the lesson. 9 out of 10 times, your problem will be answered here.

If you got the error 1120, Access of Undefined Property, then:

Make sure you spelt and capitalized the variables right. For example, you might of done MyString instead of myString. Any little thing like that causes Flash to choke.

If you got the error 1180, Call to Possibly Undefined Method, then:

Make sure you spelt trace right.

Next one coming soon!
Showing 1-1 of 1