Starting from Nothing

Variables part 1

by admin on Jan.08, 2009, under Basics

So you want to be an actionScript coder? Excellent! Our first step towards that is learning the basics syntax, structure and data types of ActionScript. Today we will be focusing on Variables.

A variable is a placeholder for information, be it a string, number, Boolean value or any other value that can change over the course of your programs runtime. A prime example of this as it relates to game programming would be a characters health. If your character started with 100 hit points and as he was damaged his hit points were reduced, this information would be stored in a variable:

1
var currentLife:Number = 100;

var : All actionScript variables are declared using the var keyword. This tells the compiler that the data to follow will be a variable.

currentLife: Is the name of our variable. The name can be any word or series of letters. As a convention, and to maintain proper coding, we will always name our variables starting with a lowercase letter and any additional words will start with an uppercase. It is also a good habit to give your variables short but descriptive names, for readability.

Number: This defines the type of data that your variable will hold. In this case we have selected number but there are other types that we will explore shortly. The data type is always declared right after the variable name and they must be separated by a colon :

= 100 : The equals sign assigns our variable with the value that follows it, in this case 100. The equals sign in this context is also known as assignment operator.

Variable can also be declared with out an assigned value, with a value given to them later in the could, like the following:

1
2
var currentLife:Number;
currentLife = 100;

As well multiple variables can be declared on the same line, using a single var keyword:

1
var  mana:Number, strength:Number, defence: Number;

You will note that every line of the above code ended with a semicolon ; this tells the compiler that the line was one instruction. You need to include a semicolon at the end of each operation. You will see this done throughout every piece of code written, so it will become second nature to you.

There is a special type a variable that does not change at all, this is called a constant. These are used to define something that we want to reference but will not change, such as days in year:

1
Const DAYS_PER_YEAR:int = 365;

As you can see, there are to main difference about the way in which we declare a constant. The first is that instead of using the var keyword we use the const keyword, this tells the compiler that our value will not change and will generate an error if we attempt to change it anywhere in our code. The second thing you will notice is the way we named it ( here after referred to as the ‘naming convention’ ) is different. As a matter of standard practice and all around good coding, we name constants by using all uppercase letters and separating words with an underscore. It is best to define something like DAYS_PER_YEAR if you are going to use it in you code, even if you know off the top of your head that there are 365 days. It will make your code easier to understand in the long run:

BAD CODING

1
var x:Number = 365 * 24 * 3600;

GOOD CODING

1
2
3
4
5
const DAYS_PER_YEAR:Number = 365;
const HOURS_PER_DAY:Number = 24;
const SECONDS_PER_HOUR:Number = 3600;
 
const SECONDS_PER_YEAR:Number  = DAYS_PER_YEAR * HOURS_PER_DAY * SECONDS_PER_HOUR;

In the above example, our bad coding practice would result in confusion for anyone trying to follow our code, even ourselves if we had not seen it in a while. Our const did not have a descriptive name and the operation we performed ( multiplication of 3 numbers ) was done with unknown reasons.
Our good coding practice defined everything according to standard naming conventions, all uppercase letters and separated by underscores for our constants and it gave them all names that would readily identify there value as well as there purpose. So if we came back to this code a month or a year later just by looking at this code we could tell what we were trying to accomplish.

4 Comments :, , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

Archives

All entries, chronologically...