|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
. . . . . |
Flash Actionscript Programming Basics
This course is adapted from the book
"C++ Primer" by Stanley Lippman.
To enter actionscript code, open the Actions window by hitting the F9 key or
selecting menu item "Window" - "Actions", and start writing in that window. var myNumber;What you have done is a simple VARIABLE DECLARATION. You have reserved space in memory where some information/data is to be stored. Now write this in place of above.: var myNumber = 10;You have assigned a value to the variable. i.e. stored the value 10 in the variable myNumber.
The trace( ) function prints whatever is between the parentheses to the output window.
It displays the value of the argument of trace.
To sequence text and variables in a trace function use "+" to add text to number and variables etc.
trace("some Text " + someVariable);
So, here is our complete script,
var myNumber = 10;
trace("The value is " + myNumber);
Now execute or run the script. To execute a script hit the keys "Control + Enter" together,
or use menu item "Control" - " Test Movie".
After a script is written, the next step is to compile it. Compiling means to write to bytecode that Flash understands.
The compiler has 2 jobs:
Flash Tutorials in Video Format -
Watch them now at LearnFlash.com Statements
A Statement is an expression terminated by a semi-colon (";").
A statement in AS is similar to a sentence in the English language. E.g. var score; score = 80; trace(score);Lets analyse the above script
Different values can be placed in the boxes for each name. The name is called a variable.
It can be created or thrown away at any time.
When you declare a variable, you set up a space in memory for it and also leave a space (connected with it)
for a variable's value.(8, 16, "Frodo", whatever). var string1 = "This is a test"; trace(string1);What did you get in the output window? Try another: var isTrue = true; trace(isTrue); CommentsComments are helpful lines that are not compiled.
There are two types.
OperatorsOperators are signs that do something to variables on either side of them. Here are the arithmetic operators:
The Modulus operator(%) computes the remainder of division between 2 integers. var x = 3; var y = 5; var mult1 = x * y; trace(x + " times " + y + " = " + mult1);ex2 var min = 35; var max = 239; var addition = min + max; trace(min + " plus " + max + " = " + addition;You get the idea. Try all the operators and start writing your own scripts. Equality, Relational and Logical OperatorsThese operators evaluate to true or false Logical AND (&&)
Evaluates to true only if both its operands evaluate to true. E.g.
x = 5;
if(x>0 && x < 10){
something = true;
}
trace(something);
BOTH MUST BE TRUE.
Logical OR ( || )Evaluates to true if either of its operands evaluate to true. E.g.
x = 5;
If(x>0 || x<4){
something = true;
}
trace(something);
EITHER CAN BE TRUE
Assignment Operator ( = )The effect that an assignment has is to store a new value in the left operand's associated memory storage space e.g. x = 7;This statement means that the value 7 is assigned to the variable x. Increment and decrement Operators ( ++ and -- )
The increment (++) and decrement (--) operators are a shortcut way of adding or subtracting 1 from a variable. E.g. stack[++top] = val; //Is equivalent to the following 2 lines top = top +1; stack[top] = val;The postfix form of ( --) decrements the value of top after that value is used. stack[top--] = val; //Is equivalent to : stack[top] = v; top = top -1; Arithmetic "IF" operator
The syntactic form is: x = 2; y = 5; max = x<y ? 100 : 10; trace(max); Bitwise Operators
Bitwise operators operate on the value to base 2 (binary). Operands must be integers.
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;
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
var num1:Number; var str1:String; num1 = "Steve"; str1 = 27; Flow controlIF
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.
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;
}
LOOPSA loop cycles over a body of code as long as the condition remains true. while
template:
while(expression){
statement;
}
The statement is executed as long as the conditions is true. E.g.
var x =0;
while(x < 10){
trace(x);
x++;
}
do .. while
a do.. while loop executes the statement once before the condition is evaluated.
do{
//Statements to be executed;
}
while(condition);
E.g.
var x =0;
do{
trace(x);
x++;
}
while (x < 10);
for
a for loop is used most commonly to step through a fixed length data structure such as an array.
for(init-statement; expr2;expr3){
// Statements;
}
e.g.
for(var i=0; i<10; i++){
trace(i);
}
the first statement initialises a looping variable3. sets the condition 3. increments the looping variable( also could be something like i=i+2) Functions
A function is a named unit, in which statements are logically grouped. It usually contains the following parts:
Function functionName(arg1, arg2, arg3){
// statements go here
return something if not void return type;
}
example1 (AS 1.0). Type this and compile (Control+Enter):
function test1(arg1, arg2){
// arg1 is passed to the variable with the same name
// in the next line I.e. arg1
trace(arg1);
trace(arg2);
}
// call function
test1(3,"Steve");
Notice that to call a function we write the function name and the argument lists in parentheses.
Template to call a function:functionName(arg1, arg2);Example 2 with a return value (AS 1.0)
function returnTest(arg1){
var num = arg1 * 10;
return num;
}
// call function
trace(returnTest(5));
ex. 3
// returns the absolute value of number
function abs(number){
return( i < 0 ? -i : i);
}
// call the method
trace(abs(-350.95);
ex 4
// return the smaller of two values.
function min(num1,num2){
return(num1 <= num2 ? num1: num2);
}
trace(min(27,45));
Functions in AS 2.0
Functions in AS 2.0 are a little different. AS 2.0 allows the setting of specific datatypes.
Here is an example :
Function avgReturn(arg1:Number):Number{
Var num = arg1 * 5;
return num;
}
The template for an AS 2.0 function could be :
Function functionName(arg1:DataType,
arg2:DataType):ReturnType{
// statements go here
return something ;
}
Void is used to specify that there is no return type. E.g.
function noReturn(arg1:Number,
arg2:String):Void{
trace(arg2+" is a string and "+arg1+" is a number");
}
// call function
noReturn(5,"Steve");
Arrays
An array is a collection of objects . Individual objects are not named but accessed by its position in the array.
Try this!
var theArray = new Array(5,4,8,2,1);
for(var i = 0;i<5;i++){
trace(theArray[i]);
}
Elements are numbered beginning with "0". In the above array theArray[0] = 5 theArray[2] = 8 Length is a property of Array. It tells how many elements are in the array. So , from above, theArray.length = 5 Classes
(As 2.0 only)
class Dog{
// data members
var name:String;
var age:Number;
// class method
function setAge(age1:Number){
age = age1;
}
}
Constructors allow us to create an instance of a class . e.g.
var rusty:Dog = new Dog();
// the dot operator (".")
// allows us to access members and methods of that class.
rusty.setAge(7);
rusty.name = "Rusty";
Recommended Book
Go here for more information on classes and OOP programming . .. |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
. | Home | Flash MX | Actionscript 2.0 | Flash 3D | Flash 8 | Flash Database | Flash Mobile | Flash CS3 | Java For Kids | Video Course | General Video | Photoshop | Web Design | Digital Photography | Games | free backgrounds | Resume | Streaming Video | Students Work | Links | Contact me | sitemap | reviews | . . |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||