Beginners Flash Tutorial – ActionScript Operators
November 14, 2008
For users who prefer to learn Flash visually we have a range of Adobe Flash CS3 video tutorials, this method of training greatly enhances learning and allows beginners to master even the most complex aspects of Adobe Flash with ease.
View the Beginners Flash CS3 Tutorial Video
Beginners Flash Tutorial – ActionScript Operators
Conditionals
In this Flash tutorial we’ll look at decision-making in Actionscript 3.0 using conditional statements. For example you might want a certain piece of code to execute under a certain circumstance and another piece of code to execute under different circumstances, ActionScript 3.0 like most programming languages uses conditionals to deal with these situations. To simplify ActionScript creates a test asking whether conditions are met, if the condition is met the test will evaluate to true and if the condition is not met alternative action is taken. In these examples we’ll be using some symbols known as comparison and logical operators. Below is a list of both types of operators you should familiarise yourself with before going any further.
Download the working files used in this tutorial »
Comparison Operators
== is equal to
< is less than
> is greater than
>= is greater than or equal to
<= is less than or equal to
!= is not equal to
Logical Operators
%% And
|| Or
! Not
If and Else Statements
For these examples we’re going to use some TextField and TextFormat properties to help explain the code a bit better, below these objects are briefly explained.
We create a new TextField object and call it theText. This creates an instance of the TextField class and gives us access to all the properties, methods and events of that class.
var theText:TextField = new TextField();
Next we create a TextFormat object called theFormat, again giving us access to all the properties, methods and events available to that class. The TextFormat class will allow us to change the colour properties.
var theFormat:TextFormat = new TextFormat();
Setting the colour format to red using the color property.
theFormat.color = 0xFF0000;
Populating the TextField with the word “hello” using the text property.
theText.text = "hello";
Combining the TextFormat properties with the TextField.
theText.setTextFormat(theFormat);
Adding the TextField to the stage using the addChild() method, making it visible when the file is published or previewed.
addChild(theText);
The if Statement
The first condition we’ll look at is the “if statement”, with if statements your essentially asking if something is true or not. To use the “if statement” we start out with the “if” keyword to declare that we are using an “if statement”. Inside the brackets is the condition and inside the curly braces is the code that’ll execute if the condition inside the curly braces is met. For our code below we’re asking if the text inside the theText TextField is equal to “hello”, it is so therefore the code inside the curly braces is executed and the text is traced out in the output window when the file is previewed.
Example;
if(theText.text == "hello")
{
trace("the text inside the text field says hello");
}
Adding an else statement
So what happens if you want some code to execute if the code returns a false value or doesn’t add up? For this we use the else statement, the else statement executes when all the other conditions aren’t met. In our code below we’re asking if the text inside the TextField is equal to “goodbye”, it isn’t therefore the else statement is executed and the trace statement is traced into the output window when the code is previewed.
Example;
if(theText.text == "goodbye")
{
trace("The text inside the text field says goodbye");
}
else
{
trace("The text inside the text field doesn't say goodbye");
}
else if Statement
What happens if you want to execute more than one if statement? Unfortunately we can only use one if statement and one additional else statement, but there is a way around this with the use of the else if statement. Think of the else if statement as just an alternate if statement. In our code below the “if statement” returns a false value so the code moves onto the else if statement, the else if statement condition is true so the trace statement inside the curly braces is executed.
Example;
if(theText.text == "Goodbye")
{
trace("the text inside the text field says goodbye");
}
else if(theText.text == "hello");
{
trace("The text inside the text field says hello");
}
Using switch Statements
As you may have noticed if statements can be rather bulky and can take a while to read through, this can be a particular pain when coming back to old code you haven’t seen in a while. An alternative method is to use the switch statement to shorten the code, let’s convert the code from the previous example into a switch statement. In the example below we start out with the switch keyword declaring that it’s a switch statement, inside the brackets is the object we want to evaluate, the case line represents the possible value and the trace statement executes if the test is successful. Finally the break line indicates the end of each case.
Example;
switch (theText.text){
case "goodbye" :
trace("The text inside the text field says hello");
break
case "hello" :
trace("The text inside the text field says hello");
break
}
Entry Filed under: Flash Tutorial. Tags: actionscript tutorial, as3 tutorial, Flash CS3 Tutorial.
Trackback this post | Subscribe to the comments via RSS Feed