Actionscript 3.0 Fundamentals
Statements end with a ;
Variables
var name:type;
You can leave off a type, but then Flash doesn't know what type of data
you are using.
Why is it benificial to list the data type?
Primative Data types:
String A single
character, or a sequence of characters
Boolean true or false values
int
Positive and negative whole numbers
uint
Positive whole numbers
Number Positive and negative
whole and real (i.e. fractional, like 3.5) numbers.
Constants:
const DAYS_PER_WEEK:uint = 7;
Order of Operations
- Parentheses
- Exponents - Math.pow(variable, exponent)
- Multiplication and Division
- Addition and Subtraction
Unary Operations:
! logical negation
- negative
+ positive
++ increment
-- decrement
Arrays:
var myArray:Array = new Array();
var myArray:Array = new Array(5);
Passing something other than a number, or more than one value, will be
treated as values in the array.
var people:Array = new Array("Larry");
var players:Array = new Array(23,34,10);
Array literal, a comma separated list of values enclosed in square
brackets:
var people:Array = ["Lawrence", "Herbert", "Shirley"];
trace (people);
// Size of the array.
trace(people.length);
// First element
trace(people[0]);
You can add values to the array by using it's subscript or via the push
command:
people[3] = "Perry";
// Add to the end of the array.
people.push("Fay");
trace(people);
My array has now:
Lawrence, Herbert, Shirley,Perry, Fay
delete people[2];
Lawrence, Herbert, ,Perry, Fay
There is an empty values where "Shirley" was.
splice will remove a value.
var people:Array = ["Lawrence", "Herbert", "Shirley"];
// Start at position 0 and remove 1 element.
people.splice(0,1);
trace(people);
Herbert,Shirley
Decisions:
Similar to C using if-else
== is equal to
!= is not equal to
< is less than
> is greater than
<= is less than or equal to
>= is greater than or equal to
Boolean Operators
&& AND
|| OR
Loops:
Again, similar to C:
while, do-while, and for loops
var i:uint = 1;
while(i<=100) {
trace(i);
i++;
}
var i:uint = 1;
do{
trace(i);
i++;
}
while(i<=100)
postfix++
++prefix
for(var i:uint=1; i<=100; i++) {
trace(i);
}
Functions:
function name(parameters):returnType {
statements;
}
function countTo(max:uint):void {
for(var i:uint = 1; i<=max; i++) {
trace(i);
}
}
countTo(5);
countTo(10);
// You can specify defaults. Such as min has a default of 1.
function countTo(max:uint, min:uint = 1):void {
for(var i:uint = min; i<=max; i++) {
trace(i);
}
}
countTo(10);
// Here the min value is set to 5.
countTo(10,5);
Multiple variables:
function sum(...operands):Number {
var numOperands:uint = operands.length;
var total:Number = 0;
for (var i:uint = 0; i < numOperands; i++) {
total += operands[i];
}
return total;
}
trace (sum(1,2,3));
trace (sum(5,10));
Passing values and references
Primitive data types are passed by value (Numbers, Strings, Ints, etc).
Complex (such as Arrays) are passed by reference.
Scope of variables (global, local).
Comments:
//
/* */
Use commenting skills that you learned in 1.5 and 15 for function
(parameters, return values, explaination of the function).