Home » ActionScript 2.0, ActionScript 3.0

ActionScript Basics part 5

24 November 2010 No Comment
Share on Facebook

Previous Entries:
ActionScript basics part1

ActionScript basics part2

ActionScript basics part3

ActionScript basics part4

ActionScript basics part5

ActionScript basics part6

Logic is a part of our everyday lives – it’s a very familiar concept, therefore one of the easiest to understand. We are faced with questions all the time, with the “If” question being the most common. If I go to sleep early, I will be able to get up early. If I take this job, I will make more money. If I go to school, I will learn ActionScript. Using ActionScript, you can answer all of these questions using conditional statements.

Conditional statements allow you to setup the question or logic within your script, and if it is a true statement, then actions can be taken which are defined by you. Conditional statements create smarter scripts by performing an action based on something that has been setup in the program – it makes your scripts dynamic!

Similar to other languages, ActionScript offers designers and developers the use of the following conditional statements:

  • If
  • else
  • else if
  • switch
  • If Statement

An “if” conditional statement is setup with 3 parts: the if keyword that sets up the condition, which is located between the parenthesis, and the curly brackets is where the action is placed. The following is an example of how this is setup:

if (condition) {

    //action or event will go here if condition is equal to true

}

The if statement allows you to test a condition by seeing if it evaluates to true, then something will occur. The condition will often test two values (strings, numbers, and boolean values) using a comparison operator.
The following example uses the comparison operator “for is not equal to” (!=), to compare var a and 100. If the statement executes as true, the browser will display an trace message that says “Not Equal”.

var main:Number = 25;
if (main != 100) {

    trace("Not Equal");

}

If you wanted to test the same variables above to see if they are equal to each other, all you need to do is switch in the (==) comparison operator.

var main:Number = 25;
if (main == 100) {

    trace("Numbers are Equal");

}

This conditional statement will work fine in some situations, but there will be situations where you will want something else to happen if it doesn’t evaluate as true.

Else Statement

One of the main problems with using just an “if” statement is that if the condition returns false, the action will be ignored. If you are wondering what I mean, think about this scenario. If I get the job, I will take it. But what if you don’t get the job, this is where the else statement comes into play. As you could then say; If I get the job, I will take it. Otherwise, I will keep looking and interviewing for other jobs. The important thing to remember about the else statement is that it doesn’t have a condition, it is the condition.

The else statement gives you the option of doing something if the “if condition” comes back as false. In the example below, the value of foo has been set to 100. If the value of foo is not equal to 100, then show an trace that says “foo is not equal to 100!”. Otherwise show the trace “foo is equal to 100!”. Otherwise in this case means else, or the condition in the if statement returned false.

var main:Number = 100;

if (main != 100) {

    trace("main is not equal to 100!");

}

else {

    trace("main is equal to 100!");

}

Else/If Statement

If you would like to test multiple conditional statements, you can add in “else if” after the first if statement. The script will first test the first if condition, if that is false, then it will run the second else if condition, if that is still false, it will execute the statement between the last else.

var topMenu:Number = 25;

if (topMenu != 100) {

    trace("a is not equal to 100!");

}
else if (topMenu < 100) {

    trace("a is less than 100!");

}
else {

    trace("top menu rocks!");

}

You can use else/if statements to test multiple conditions, which is very useful in ActionScript. If you have a web form with 5 required fields, you can use an else/if statement to run through each field and check that something has been entered. If the user did not enter any value, you can create an trace that will show and apply the focus back to that form field. The following example code outlines how you would achieve this problem with ActionScript:

Advanced Conditional Statements

If you would like to test if multiple conditions are true within one if statement, you can do so using the logical operators.

In order to make a decision based on a variety of conditions, a logical operator to link those conditions together can be used.

In the following example: If I get the job and the salary is over 50,000, then I will take the job. The following code shows how this can be achieved in ActionScript.

var job:Boolean = true;

var salary:Number = 55000;

if (job == true && salary > 50000) {

    trace("Accept the job");

}

You can link as many conditions together as you need to, but it is important to remember that they all need to return true in order for your action within the curly brackets to occur. In the following example there are five conditions listed, although the fifth condition has an OR logical operator that precedes it. So either the first four conditions have to be true or the fifth condition has to be true, in order for the trace to show up. If none are true, then the else statement will come into effect and that trace will show.

var job:Boolean = true;

var salary:Number = 55000;

var dress:String = "casual";

var hours:Number = 8;

var lunch:Boolean = false;

if (job == true && salary > 50000 && hours == 8 && dress == "casual" || lunch == true) {

    trace("Accept the job");

}
else {

    trace("Don’t accept the job, there will be others.");

}

Switch Statement

If you were planning on testing many conditional statements, the if/else/if can get hard to manage and you will end up with a lot of repetitive code, as seen in the example above. An alternative is to use a switch statement.

Declare a variable to test against.

var job = 5;
// Set up the initial test using the switch keyword
switch(job)
/*Set up different cases to which I want to compare against the switch statement. In the following code, I am testing to see if the value of job matches any of the cases, and if so to show the trace, then stop the switch by using the keyword break.
*/

{
  case 5:
    trace("This is the one.");
    break;
  case 4:
    trace("This is the one.");
    break;
  case 3:
    trace("This is the one.");
    break;
  case 2:
    trace("This is the one.");
    break;
  case 1:
    trace("This is the one.");
    break;
}

GET THE UPDATES VIA EMAIL

We don't share your email anywhere, grab our rss via feedburner


Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.