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.
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:
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:
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;
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!’.
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.
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 is: "+goombas); trace("The name of the Goomba in goombas is: "+goombas); trace("The name of the Goomba in goombas is: "+goombas); //this will print out: //The name of the Goomba in goombas is: Goomba Steve //The name of the Goomba in goombas is: Goomba Bob //The name of the Goomba in goombas 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.
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:
 = “timmy”,
 = “johnny”,
 = “suzy”,
 = “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.
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.