gavin sim logo
 

Week 1

Week 2

Week 3

Week 4

Week 5

Week 6

Week 7

Week 8

Week 9

Teaching > ActionScript

Conditionals

Quick Example

Create a new flash document and on the first frame type the following code:

var animal:String="Dog";
if(animal=="Dog"){
		trace("Its  a dog");
	} else {
		trace("It's  not a dog");
}
  

If you Control / Test Movie you will see that in the output window It's a dog is displayed, this is because the first condition is met. If you change the variable animal to something else you will see that the second condition (else) is ouput.

 

Quick Example

This is a an example of a if statement checking the content of a input box.

Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player

 

Main Tutorial

This tutorial aims at looking at the use of conditionals within Flash. You will develop a simple login screen (the username is scoobydoo and the password icecream)

Start by creating four layers and 'rename them text, buttons, actions and labels'.

In the text layer create two static text boxes and enter the text "Enter username" and "Password". Place them underneath each other with Enter username on top.

Create two input text boxes with the instance names userNameDT and passwordDT (make sure you select the show borders around text option).

Create a dynamic text box under the existing text and alter the line type to multiline and give it a variable name messageDT.

In the buttons layer create a submit button and give it an instance name of submitBT. Your interface should now look like below.

infercae layout

Create key frames in frame 2 of each layer. Label frame 2 of the labels layer "startpage" this is entered into the properties panel under frame (Labels are a useful method of referencing a location within the movie).

start page

Create a static text box and enter the text "welcome". Select this using the arrow tool and convert it to a movie clip (F8) naming it "welcome". Then delete it from the stage by selecting it and pressing delete.

Create a blank movie clip by using the option insert/new symbol and called it "wel" and select the welcome movie clip from the library F11. Then place an instance of this clip on the stage on frame 1 of the new movie clips timeline.

Select / Insert Motion tween.

Extend the duration of the animation to 40 frames.

Then open the motion editor, select the color option and alpha. Reduce the value to 0.

alpha

 

Then move the playhead to frame 40 and adjust the value to 100.

 

Create another layer called actions and insert a blank keyframe on frame 40 of this layer and add the following code.

stop(); 

Finally return to the main timeline, select the text layer, frame 2, and place the 'wel' movie clip onto the stage.

Now on frame one initialise the variables that are to be used in the movie by declaring them.

Attach the following code to the frame 1 of the actions layer:

//init
var correctPassword:String="icecream";
var validId:String="scoobydoo";
stop();

This assigns the string value icecream to the variable named correctPassword and scoobydoo to validId and then stops the movie.

Above the stop attach the following code: Note: You will have to create the condition for the password

submitBT.addEventListener(MouseEvent.CLICK, checkPassword);


function checkPassword(event:MouseEvent):void{
   trace(userNameDT.text.toLowerCase());
   if (userNameDT.text.toLowerCase()!=validId){
       messageDT.text="Invalid user name, please retry.";
		// create the code for the password
     } else if (    ){
        messageDT.text="Invalid password, please retry.";
         }else {
             gotoAndPlay("startpage");
         }
}

You should be familiar with the event handler so we will just focus on the remaining code.

This code is testing for a specific condition and the statement is executed if the condition resolves to a Boolean true. Otherwise the script continues to the next line after the }.

The first line retrieves the value entered into the text box with the instance name userNameDT, converts this string to lower case and compares it to the data contained in the variable validId. A 'not equals to' operator is used and a Boolean value of true is returned if the values are not equal. When a Boolean value of true is returned the second line of the code is executed, displaying the message on the screen.

If the first condition is false this will cause the parser to move to the else if condition. This allows programs to make multiple comparisons against a condition.

The second condition being checked is if the password entered is not equal to the correct password and if this is true then the message is displayed. This is the code you will have to create!

If neither of these conditions are met the user must have authorised access to the site, therefore an else condition is used to allow entry. The parser checking the actionscript code will execute this code if the first conditions are false. The movie play head will be sent to the frame label startPage by the gotoAndPlay function.

Note that an opening curley bracket must always have a closing one.
If (condition){
Conditional statement;
}

Control / Test Movie