Beginning Flash and ActionScript Game Programming Part 3: Basic Programming Concepts-Variables

In the previous section, we downloaded and installed FlashDevelop, created our first program project, and traced out some text to the console, which is used for debugging your program.

In the next few section, we’ll look at basic programming concepts you need to know to program in ANY language.

In this section we’ll learn about variables.

Variables

The first concept that you need to know about are variables.

A variable is something we use to store a value. You can think of it as just a name, that holds a value.

For instance, if we wanted to store the current score of a player, we would use:

var current_score:int=1000;

This is the method for creating any variable. First we need to use the ‘var’ text to say that we’re creating a variable, then a space and whatever we want to name our variable, then a colon (:) and the type of variable this is(explained below), and finally an equal sign and the actual value we want to assign this variable. We must end with a semicolon to let the compiler know that that is it.

So in the above example, we create a variable named ‘current_score’, of type ‘int’ (for integer), with the value of 1000.

Basic Types of Variables

There are a few basic types of variables that you can use:

  • int
  • Number
  • Boolean
  • String
  • Array

Integer (int)

An int, or integer can be any whole number IE, any number except a decimal. 1, 500, -56 are all integers.

Decimal Number (Number)

A Number on the other hand, is any number that needs a decimal in it. For instance 1.3, -250.69, -43.0.

Yes, you can put whole numbers as Numbers as well, but it’s more efficient to put them as the correct type.

In other languages such as C++, they use float (floating point numbers), and double types that represent decimal values.

Boolean (True or False)

The ‘Boolean’ type is used to represent true or false (also represented as 1 and 0).
These can be used when checking something using conditionals later on (if something is true, do something), such as if an enemy has been hit, or if the game is now over.

An example of creating some Boolean variables:

var enemy_is_dead:Boolean = false;
var fergie_is_attractive:Boolean = false;
var pizza_is_ambrosia:Boolean = true;

Text (String)

A String is used to store any text, from a single character, a word, to several pages of text.
String variables MUST have single or double quotes around the value(‘ or “), such as: var my_address:String = “5242 S. Applewood Rd, Redwood, CA”.

Some examples of Strings are: ‘h’, ‘Game Over’,’ the correct value is = 17′, ’43’, ‘Welcome to Ninja Pigs! Ninja Pigs is a game where you throw pigs at carmel coated space ballons!’.

Manipulating Variables

You can combine variables, and use their values in any way you need. You can loop up more functions that strings are able to perform on Adobe’s live documents, but here are some examples.

var jimmys_apples:int=4;
var suzys_apples:int=12;
var all_apples = jimmys_apples+suzys_apples;

trace("jimmy has "+jimmys_apples+" apples, suzy has "+suzys_apples+", all the apples = "+all_apples);

// these two forward slashes make the whole line commented out - so you can read them, but the compiler ignores them
//traces out: jimmy has 4 apples, suzy has 12, all the apples = 16

//and adding strings:

var noun1:String="Jon";
var noun2:String="the Cat";
var noun3:String="the Spoon";

var sentence1:String = "Why did ";
var sentence2:String = " use the litter box?";

trace(sentence1+noun1+sentence2);
trace(sentence1+noun2+sentence2);
trace(sentence1+noun3+sentence2);

trace("All the names are: "+noun1+", "+noun2+", "+noun3);

//which prints out:
//Why did Jon use the litter box?
//Why did the Cat use the litter box?
//Why did the Spoon use the litter box?
//All the names are: Jon, the Cat, the Spoon

As you can see, you can perform normal addition, subtraction, division, multiplication on integers(int) and numbers(Number), and you can add Strings using the “+” character. Combining strings together is also known as “concatenating” them.
Strings have their own methods for being cut apart and put back together, which you can find on Adobe’s live docs, or in later chapters.

Array

An array is a list of objects, such as a list of numbers, or words, any object you want.
The advantages to using an array is being able to put simmilar objects in them, and loop through them, performing a repeated action on all of them.
For instance to check which goombas are dead in Mario, you would simple loop through the array, and check if each one is dead.

To create an array, you use:

var goombas:Array = new Array(); //my normal way of creating a new blank array

Which will create a new, empty array object. You can also create an empty array instead using:

var goombas:Array = []; //an alternative way of creating a new blank array

Now that you initialized the array (using new Array()), you can add stuff into it.

So lets add some names of the goombas to our array:

var name0:String = "Goomba Steve";
var name1:String = "Goomba Bob";
var name2:String = "Goomba Bruce";

goombas.push(name0);
goombas.push(name1);
goombas.push(name2);

In the first 3 lines, we’re just creating ‘String’ variables (named nameX) with values assigned to them (Goomba …).

The next 3 lines, we’re using the array function (we’ll learn more about functions later) ‘push’. This just adds the value into the end of the array.

In programming, counting starts at zero. So when counting objects, you count ‘0, 1, 2, 3, ect’. So if we want to see what is in this array we can use:

trace("The name of the Goomba in goombas[0] is: "+goombas[0]);
trace("The name of the Goomba in goombas[1] is: "+goombas[1]);
trace("The name of the Goomba in goombas[2] is: "+goombas[2]);

//this will print out:
//The name of the Goomba in goombas[0] is: Goomba Steve
//The name of the Goomba in goombas[1] is: Goomba Bob
//The name of the Goomba in goombas[2] is: Goomba Bruce

So when we trace (print out to the console where the end user won’t be able to see) out each of the spots in the ‘goombas’ array, we see each of the strings we put in there, one after another.

We use square brackets ( [ and ]) to access an element in an array. The first element is zero, the last element is how many items you have in the array minus 1, since it starts at 0. So if an array has 5 elements, the last one would be in position: array.length-1.

We can store any objects in arrays, but a good rule is to only store objects of the same type.
You don’t want to push number, integers and strings into the same array. If you try to do something to one of them, you’ll encounter problems, since each object type works differently.

Creating a Non-Empty Array

Instead of having to use ‘push’ to add things to an array, you can also put thing into the array when you create it.

An example:

var names:Array = new Array("timmy", "johnny", "suzy", "billy");

//or an alternative syntax that does the same thing:
var names2:Array = ["timmy", "johnny", "suzy", "billy"];

Either of these methods can be used to create a new array, with elements already added to them.
The arrays themselves will look like:

names=
[0] = “timmy”,
[1] = “johnny”,
[2] = “suzy”,
[3] = “billy”

So you can see that it did the same thing as needing to push them into the array, one at a time.

Valid Variable Names

For all variables, the variable name has to be alpha-numeric, and start with a letter.
No spaces, no special characters (aside from underscore).

Valid names include:
‘appple’, ‘A321’, ‘my_pizza_delivery_business’

Invalid names include:
‘123a’, ‘my bathroom’, ‘:D’, “this_sucks******”

You also can’t use predefined names, built into flash, such as:
‘var var’, ‘var int’.

FlashDevelop will throw an error if you try to compile with an invalid variable name, and will let you know which line the error happened at, so you can easily find it and fix it.

Conclusion

Now would be a good time to start learning about ‘loops’, which allow you to quickly go through your array, and do repeated actions to objects in the array.

But before that, we first need to learn about conditionals – being able to check something, and make choices.

In the next section, we’ll go over conditional statements in programming- making choices.

Other Articles in this Series

6 thoughts on “Beginning Flash and ActionScript Game Programming Part 3: Basic Programming Concepts-Variables”

  1. Hey, very good article ! thanks !

    there is a little mistake here:
    var name0:String = “Goomba Steve”;
    var name1:String = “Goomba Bob”;
    var name2:String = “Goomba Bruce”;

    goombas.push(name1);
    goombas.push(name2);
    goombas.push(name3);

    it should be:

    goombas.push(name0);
    goombas.push(name1);
    goombas.push(name2);

    btw thanks for this article !

    1. Ah, thanks for catching that Tadeu!

      I appreciate the feedback, I updated the mistake in the code above.

      Thanks for taking the time to read and comment on the article!

      Let me know if there are more things you’d like more articles on!

  2. Great articles Chris, they are easy to follow and understand.

    I did find one typo

    var noun1:String=”Jon”;
    var noun2:String=”the Cat”;
    var noun2:String=”the Spoon”;

    Should be:

    var noun1:String=”Jon”;
    var noun2:String=”the Cat”;
    var noun3:String=”the Spoon”;

    Keep up the good work!!

    1. Awesome, thanks for going through them John, updated and fixed the typo!

      Going to have to go through all of them in this series when finished and fix any mistakes, as well as add a couple of demos to “test your knowledge”/ reinforce concepts.

      Thanks!

  3. This site is great! It answers so many questions I have been asking myself on other sites… I am grateful you have created such an excellent reference website for flash-beginners –like me! 🙂

Leave a Comment

Scroll to Top