ActionScript 3.0 Basic Programming Concepts by Martin Stanhope The University of Bolton.

19
ActionScript 3.0 Basic Programming Concepts by Martin Stanhope The University of Bolton

Transcript of ActionScript 3.0 Basic Programming Concepts by Martin Stanhope The University of Bolton.

Page 1: ActionScript 3.0 Basic Programming Concepts by Martin Stanhope The University of Bolton.

ActionScript 3.0

Basic Programming Conceptsby

Martin StanhopeThe University of Bolton

Page 2: ActionScript 3.0 Basic Programming Concepts by Martin Stanhope The University of Bolton.

What is AS3?• ActionScript 3.0 (AS3) in an object-oriented

programming (OOP) language used to create Flash movies (swf files). It is integrated into the Abobe Flash CS5 application.

• ActionScript has been developed over the years from AS1 to AS2 and now, with the latest releases of Adobe Flash, it has matured to become version 3.0.

• AS3 is a major step forward from AS2 in that it has become a full-featured object-oriented programming language.

Page 3: ActionScript 3.0 Basic Programming Concepts by Martin Stanhope The University of Bolton.

Program comments• Always place comments in your AS3 program

code to indicate to the reader what the code is for.

• Single line comments begin with //• Multiple line comments are bounded by/* and */• The multiple line method is very useful for

temporarily ‘commenting out’ a section of code during debugging.

Page 4: ActionScript 3.0 Basic Programming Concepts by Martin Stanhope The University of Bolton.

Constants, variables and data types• Constants. Values that do not change (i.e. remain constant)

can be assigned to meaningful names which can then be used throughout the program to represent the value. E.g. const VAT = 17.5;Capital letters are normally used for constants so they are obvious to the program reader.

• Variables are named memory locations used to store data of a specific type (uint, int, Number, String),

e.g. var playerName:String = “Captain Scarlet”;var age:uint = 30;var bodyTemperatureDegC:Number = 36.9;

Page 5: ActionScript 3.0 Basic Programming Concepts by Martin Stanhope The University of Bolton.

Simple debugging using trace()• Trace is an AS3 built-in function giving the

developer a means of displaying program output (e.g. the contents of variables) to an output window for the purpose of program debugging and development.

// A simple example of the use of the trace functiontrace(“Player’s name is “ + playerName);trace(“Current age is “ + age);trace(“Body temperature is “ + bodyTemperatureDegC + “ degrees

Celcius.”);

Page 6: ActionScript 3.0 Basic Programming Concepts by Martin Stanhope The University of Bolton.

Operators

• Arithmetic +, -, *, /, %• Conditional ==, !=, <, <=, >, >=• Logical &&, ||, ! • Bitwise << , >>, &, |, ^, ~• Assignment =• Reassignment +=, -=, *=, etc...

Page 7: ActionScript 3.0 Basic Programming Concepts by Martin Stanhope The University of Bolton.

Operators - ArithmeticArithmetic operators... +, -, *, /, %Example:var x:int = 6;var y:int = 4;var z:Number = 0; // Note the data typez = x + y; trace(z); // Displays 10z = x - y; trace(z); // Displays 2z = x * y; trace(z); // Displays 24z = x / y; trace(z); // Displays 1.5z = x % y; trace(z); // Displays 2, the remainder

Page 8: ActionScript 3.0 Basic Programming Concepts by Martin Stanhope The University of Bolton.

Operators Precedence and Associativity

• Operator precedence: The order of operation

trace( 2 + 3 * 10); // displays 32 not 50trace( 12 / 4 – 2); // displays 1 not 6

• Operator associativity (left or right ?)

trace( 2 * 6 / 3); // displays 4 as the operators // of the same precedence work left to right

z = x = y; //The assignment operator

trace(z); // uses right to left associativity so this displays 4

Page 9: ActionScript 3.0 Basic Programming Concepts by Martin Stanhope The University of Bolton.

Using the + operator with strings

• The + operator is also used to concatenate (join) strings together as shown below...

var firstname:String = “Joe”;var lastname:String = “Bloggs”;var email:String = “[email protected]”;trace(“Student details are “ + firstname + “ “ + lastname + “ “ + email);

Page 10: ActionScript 3.0 Basic Programming Concepts by Martin Stanhope The University of Bolton.

Operators - Conditional• Conditional operators: ==, != , < , <=, >, >= var x:uint = 6;var y:uint = 4;var s1:String = “abc”;var s2:String = “bcd”;var result:Boolean;

result = ( x == y ); trace(result) ; // displays FALSEresult = ( x != y ); trace(result) ; // displays TRUEresult = ( x < y ); trace(result) ; // displays TRUEresult = ( x <= y ); trace(result) ; // displays TRUEresult = ( x > y ); trace(result) ; // displays FALSEresult = ( x >= y ); trace(result) ; // displays FALSEresult = ( s1 < s2); trace(result) ; // displays TRUE

Page 11: ActionScript 3.0 Basic Programming Concepts by Martin Stanhope The University of Bolton.

Operators - Logical• Logical (Boolean) operators: &&, ||, !var a:Boolean = true;var b:Boolean = false;var c:Boolean = true;var result:Boolean;result = ( a && b); trace(result); //displays falseresult = ( a && c); trace(result); //displays trueresult = ( a || b); trace(result); //displays trueresult = ( a || c); trace(result); //displays trueresult = !a; trace(result); //displays falseresult = !b; trace(result); //displays true

Page 12: ActionScript 3.0 Basic Programming Concepts by Martin Stanhope The University of Bolton.

Operators - Bitwise

• Bitwise shift left <<• Bitwise shift right >>• Bitwise AND &• Bitwise OR |• Bitwise Exclusive OR (XOR) ^• Bitwise inversion (NOT) ~

Page 13: ActionScript 3.0 Basic Programming Concepts by Martin Stanhope The University of Bolton.

Operators – Bitwise - Examplesvar a:uint = 8; // Binary 00001000var b:uint = 12; // Binary 00001100var result:uint;

result = ( a << 1); trace (result); //displays 00010000 i.e 16

result = ( a >> 1); trace (result); //displays 00000100 i.e 4

result = ( a & b); trace (result); //displays 00001000 i.e 8

result = ( a | b); trace (result); //displays 00001100 i.e 12

result = ( a ^ b); trace (result); //displays 00000100 i.e 4

result = ~a; trace (result); //displays 11110111 i.e.247

Page 14: ActionScript 3.0 Basic Programming Concepts by Martin Stanhope The University of Bolton.

Assignment operator =Assignment occurs right to left...

i. At the time a variable is declared…var degF:Number = 98.4;

ii. At a later stage within the program…

degF = 99.2;

iii. Using a value returned by a function degC = convertToCentigrade(degF);

Page 15: ActionScript 3.0 Basic Programming Concepts by Martin Stanhope The University of Bolton.

Reassignment operators

• There are numerous reassignment operators such as: += , -=, *= etc. and they behave as follows:

var x:int = 2; x = x + 5 ; // x now holds 7x += 5; // x now holds 12x -= 3; // x now holds 9

Page 16: ActionScript 3.0 Basic Programming Concepts by Martin Stanhope The University of Bolton.

AS3 program structure - MyTest1.as

package {

import flash.display.Sprite;public class MyTest1 extends Sprite {

public function MyTest1() {

//This shows a message in a debugging window

trace(“Hello World – AS3 has arrived!”);}

}}

Page 17: ActionScript 3.0 Basic Programming Concepts by Martin Stanhope The University of Bolton.

AS3 program structure - MyTest2.aspackage {

import flash.display.Sprite;

public class MyTest2 extends Sprite {

public function MyTest2() {

//Displays 100 lines drawn between random points on the stage

graphics.lineStyle(1, 0, 1); //Set the line style

for(var i:int ; i<100 ; i++){

graphics.lineTo( Math.random()*300, Math.random()*300); }

}}

}

Page 18: ActionScript 3.0 Basic Programming Concepts by Martin Stanhope The University of Bolton.

AS3 code• At this point you won’t understand the structure of

the ActionScript code, but note the following…

– The program filename has the extension .as– The filename agrees with the name of the class and the

name of the function within the class– To test the operation of, say, MyTest1.as you will need to

create a Flash application named MyTest.fla and in it set the ‘Document class’ parameter to MyTest1.

– When the work is ‘published’ the file MyTest1.swf file is created (i.e. the final goal).

– You will be amazed at how Flash movies can be created by using programming code instead of by drawing on the timeline.

Page 19: ActionScript 3.0 Basic Programming Concepts by Martin Stanhope The University of Bolton.

File locations

• Get organised before you start.• Create a folder for this module.• Create a subfolder named as3work