Agenda
Simple Types Variables Assignment Boolean Comparisons Control Structures Branching and Looping Introduction to Arrays
B.Sc. Multimedia ComputingMultimedia Authoring
Why Learn ActionScript ?
More control over movie clips and their properties Animate elements independently of the timeline Import and export data dynamically Dynamic control of sound and video Create objects that have physics-driven behaviors Personalize the user’s experience
B.Sc. Multimedia ComputingMultimedia Authoring
Structure and Syntax
Semicolons ; Curly Braces {} Code Blocks Dot Syntax ( for functions “methods”) Parenthesis Style
Script Spacing and Layout Comments
B.Sc. Multimedia ComputingMultimedia Authoring
ActionScript 3.0 Typing
Strict data typing aVariable = 5; // no type given, compiler error aVariable:Number = 5 // declare as being a number type
Case sensitive myValue not the same as myvalue
Class structures for custom objects
B.Sc. Multimedia ComputingMultimedia Authoring
Comments // this is a comment on one line
/* these are comments spanning more than one line*/
/* a script to calculate the area of a circle*/var area : Number; // holds the value of the areavar PI : Number = 3.142: // sets the value of PIvar radius :Number = 5 ; // sets the value of PIarea = PI * radius * radius // calculates the area and assigns it to the variable
B.Sc. Multimedia ComputingMultimedia Authoring
Semicolons ; and Braces {}
Semicolons separate statementsstatementOne;statementTwo;statementThree;
Curly braces define code blocks{statementOne;stateMentTwostatementThree}
B.Sc. Multimedia ComputingMultimedia Authoring
Operators
Binary + - * / Logical AND &&
(5 < 2) && (3 > 1) is False Logical OR ||
(5<2) || (3>1) is True Equalty <, >, <=, >= Assignment
a = 5
B.Sc. Multimedia ComputingMultimedia Authoring
Equality ==
a = ‘5’
x = 5
if (a == x) is True
a = 5
x = ‘6’
if (a == x) is False
B.Sc. Multimedia ComputingMultimedia Authoring
if statements
if(condition) {
statement(s);}e.g.var firstNumber:int = 25;var secondNumber:int = 39;
if(firstNumber==secondNumber){
trace("Snap!");}
B.Sc. Multimedia ComputingMultimedia Authoring
if..else
B.Sc. Multimedia ComputingMultimedia Authoring
var firstNumber:int = 25;var secondNumber:int = 39;
if(firstNumber==secondNumber){
trace("Snap!");}
else { trace("Not a match");}
switch
B.Sc. Multimedia ComputingMultimedia Authoring
var thisMonth:int = new Date().getMonth(); switch (thisMonth) {
case 0 : trace( "January"); break; case 1 : trace("February"); break; case 2 : trace( "March"); break; default: trace(“Some other month”);} // endSwitch
Looping: while
index = 4;while (index > 0){
// do something hereindex = index -1;// shorthand index --
}
B.Sc. Multimedia ComputingMultimedia Authoring
Looping: do while
int:index = 4;do {// do something here
index = index -1;// shorthand index --
} while (index > 0);
B.Sc. Multimedia ComputingMultimedia Authoring
How to Store Five NumbersUse individual variables for each number stored:
var numberOne:numbervar numberTwo:numbervar numberThree:numbervar numberFour:numbervar numberFive:number
messy - have to process as individual items ratherthan as a collection of grouped items. Solution - use an Array - contiguous data storage
B.Sc. Multimedia ComputingMultimedia Authoring
Arrays and Data Storage
B.Sc. Multimedia ComputingMultimedia Authoring
15
95
14
70
23
Elements
Size of the array is 5
The value stored at array element number 2 is 14
Array element numbering starts at 0
0
3
2
4
1
Arrays and Data Storage
B.Sc. Multimedia ComputingMultimedia Authoring
15
23
14
70
5
Array of numbers (integers)
Gibson
Fender
Punk
Martin
Array of strings
Les Paul
Declaring Arrays in Flash
B.Sc. Multimedia ComputingMultimedia Authoring
// create a new array
var numberArray:Array = new Array();
// store the numbers into the array// 15 95 14 70 23
numberArray[0] = 15;numberArray[1] = 95;numberArray[2] = 14;numberArray[3] = 70;numberArray[4] = 23;
Looping through an Array
B.Sc. Multimedia ComputingMultimedia Authoring
// total the numbers stored in the number array
var index:Number;var sum:Number;sum = 0;
for (index = 0; index <= 4; index = index + 1){sum = sum + numberArray[index]
}trace("The sum of the numbers in the array is " + sum)
Looping through an Array 2
B.Sc. Multimedia ComputingMultimedia Authoring
// total the numbers stored in the number array using array length method
var index:Number;var sum:Number;sum = 0;
for (index = 0; index < numberArray.length; index++){sum = sum + numberArray[index]
}trace("The sum of the numbers in the array is " + sum)