dcsimg
First time here? First time here? Newsletter Signup Newsletter
Search tutorials
Author: Steve Happ | Website: http://www.video-animation.com

Arithmetic "IF" operator

The syntactic form is:
Expression1 ? expr2 : expr3 ;
If expression1 evaluates to a true condition, then expression2 is evaluated, otherwise expression3 is evaluated. E.g.

x = 2;
y = 5;
max = x

Bitwise Operators

Bitwise operators operate on the value to base 2 (binary). Operands must be integers.
Left Shift ("<<") - shifts the bits of the left operand some number of positions to the left. e.g.

someNum = 1; 
0 0	0 0	0 0 0 1

SomeNum = someNum<<1;
0 0 0 0 0 0 1 0

SomeNum = someNum<<2;
0 0 0 0 1 0 0 0

Right Shift (">>")
SomeNum = someNum >>3;
0 0 0 0 0 0 0 1
The leftShift operator inserts "0" from the right.
The Right Shift operator inserts "0" from the left.

Variables (AS 2.0)

A variable is identified by a user-supplied name. Each variable is of a particular data type. E.g.

Var num:Number;
"Number" is a type specifier. A declaration can follow the template:
var variableName:DataType;
  • The use of the ":Datatype" is not strictly enforced in AS2 but I strongly recommend it.
  • A variable can be composed of letters, numbers and underscore. Upper and lower case letters are different.
  • Variables need to be initialised. I.e. given an initial value.
    var myNum:Number;	//Declaration 
    myNum = 0;		//Initialisation
    
  • In AS 1.0, variables are not strictly typed and can change types.

DataTypes (AS 2.0 only)

Variables in AS 2.0 can be given strict datatypes such as a Number or a String or a Boolean. e.g.

var myNum:Number;
var myString:String;
var isTested:Boolean;
In AS 1.0 there is no strict datatyping. A number can become a String and vice versa. e.g.
num = 10;
num = "Test";
Strict datatyping can capture errors.

Compiler Errrors

  1. Syntax errors - e.g. var numberOne:
  2. Spelling Mistakes - var mc:MoveiClip;
  3. DataType Errors - (AS 2.0)

var num1:Number;
var str1:String;
num1 = "Steve";
str1 = 27;

Flow control

IF

An if statement tests a condition. If it is true, then a set of actions is executed. Otherwise, the set is ignored or bypassed. Here is a template :

	If(expression){
		Statement1;
	}
Here is an example :
	 var x = 23;
	if(x>3){
		trace("x is greater than 3");
	}

IF .. ELSE

If .. else allows for a kind of either .. or condition.
Template:

If(expression){
	Statement1;
	}else{
	statement2;
	}
	
If expression is true then statement1 is executed.
If expression is false then statement2 is executed.
e.g.
var x = 2;
if(x > 3){
trace("condition is true");
}else{
trace("condition is false");
}

Switch (AS 2.0 only)

A switch statement is an alternative to complex nested if .. else statements

switch(variable){
	case 1:
		//Statement1;
		break;
	case 2:
		//Statement2
		break;
	default:
		//statement3;
		break;
	}
The variable to be evaluated is inserted into the switch argument. If the first variable's value is 1 then statement1 is evaluated. A break statement is inserted after it so that the switch statement is terminated. The default label is the condition for "everything else".
var tester:Number;
tester = random(3);
switch (tester) {
case 1 :
	trace("tester = 1");
	break;
case 2 :
	trace("tester = 2");
	break;
case 3 :
	trace("tester = 3");
	break;
default :
	trace("Out of range");
	break;
}

» Level Basic

Added: 2005-08-10
Rating: 8 Votes: 12
(10 being the highest)
» Author
Steve Happ is the founder of Steve's Tutes and has been going hard at it developing Flash for Web and CD-rom.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.