Actionscript - Introduktion

Download Report

Transcript Actionscript - Introduktion

Introduction to
Flash ActionScript
Statements & loops
Conditional Statements

A way for the computer to make a choice based on some condition
”If it is dark, then light the lamp, if not, leave it off.”

The if - statement is the most important conditional statement in
ActionScript
if (statement_one operator statement_two){
//run code
}
If & else statement
Example of a if-statement with the operator “>”
(If var1 is greater than var2)

if(x > 100){
trace(”X is too big.”);
}

Instead of writing another if-statement you can use the elsestatement to catch when the condition evaluates to false
if(x > 100){
trace(”X is too big.”);
}else{
trace(”X is not too big.”);
}
Operators

Relational and Equality operators, evaluates to true or false
Operator Function
==
>=
>
<=
<
!=
&&
||

If
If
If
If
If
If
If
If
var1 is equal to var2
var1 is greater than or equal to var2
var1 is greater than var2
var1 is smaller than or equal to var2
var1 is smaller than var2
var1 is NOT equal to var2
var1 and var2 exist or both var1 and var2 are both set to true
either var1 and var2 exist and/or are set to true.
Comparison operator: compares two expressions for equality
expression1 eq expression2
If(input1 eq input2){
Do someting…
Examples of usage…
if(hungry && !thirsty){
trace("go get a combo from the closest burger joint");
}
if((variable_1 == variable_2) && (variable_3 > variable_4)){
trace("variable 1 is equal to variable 2 and variable 3 is greater
than variable 4");
}
if(theTimeOfDay == theMorning){
trace("wake up");
} else if(theTimeOfDay == myBedTime){
trace("go to bed");
} else if(iShouldBeAtWork){
trace("working right now");
} else {
Trace("watch some tv");
}
Loops

ActionScript loops are used to execute a segment of code
repeatedly for a number of times or while a certain condition is
satisfied

This can save time and effort by not having to type the same code
multiple times to repeat a process

Example of loops:
For - loop
While - loop
Do/While loop
For - loop

The for-loop is probably the most commonly used because it is the
most compact and the easiest to understand and use

Structure
for (counter; condition; action){
statements;
}

Example
for (i=0; i < 10; i++){
trace ("This code is repeated ten times");
}
While - loop

The While - loop repeats a set of code as long as the condition
specified is true

Structure
while (condition) {
statements
}

Example
var i = 1;
while (i < 5){
trace ("This code is repeated");
i++;
}
Do - While loop

A Do While loop will take a comparison statement and as long as
the statement returns true, the loop will run

Structure
do {
statements;
} while (condition);

Example
var i = 1;
do {
trace ("This code is repeated");
i++;
}while(i<5);
Examples of loop Usage

Loops are very often used to manipulate and access the content of
arrays
var movie_array = new Array();
movie_array = [“Batman", “The Godfather", “Star Wars", “Lord of The
Rings", "Titanic"];
for (i=0; i < movie_array.length; i++){
trace (movie_array[i]);
}

Another common use of loops involves the creation and control of
dynamic movieclips
for (i = 1; i < 6; i++){
duplicateMovieClip("movie_mc", "new_“ + i, i);
this["new_“ + i]._x = this["new_“ + i]._width * i;
}