Download presentation
Presentation is loading. Please wait.
Published byJoseph Flynn Modified over 9 years ago
1
ActionScript 3.0 Basic Programming Concepts by Martin Stanhope The University of Bolton
2
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.
3
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.
4
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;
5
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 function trace(“Player’s name is “ + playerName); trace(“Current age is “ + age); trace(“Body temperature is “ + bodyTemperatureDegC + “ degrees Celcius.”);
6
Operators Arithmetic +, -, *, /, % Conditional ==, !=,, >= Logical &&, ||, ! Bitwise >, &, |, ^, ~ Assignment= Reassignment+=, -=, *=, etc...
7
Operators - Arithmetic Arithmetic operators... +, -, *, /, % Example: var x:int = 6; var y:int = 4; var z:Number = 0;// Note the data type z = x + y; trace(z); // Displays 10 z = x - y; trace(z); // Displays 2 z = x * y; trace(z); // Displays 24 z = x / y; trace(z); // Displays 1.5 z = x % y; trace(z); // Displays 2, the remainder
8
Operators Precedence and Associativity Operator precedence: The order of operation trace( 2 + 3 * 10); // displays 32 not 50 trace( 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
9
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 = “jb99gcc@bolton.ac.uk”; trace(“Student details are “ + firstname + “ “ + lastname + “ “ + email);
10
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 FALSE result = ( x != y ); trace(result) ; // displays TRUE result = ( x < y ); trace(result) ; // displays TRUE result = ( x <= y ); trace(result) ; // displays TRUE result = ( x > y ); trace(result) ; // displays FALSE result = ( x >= y ); trace(result) ; // displays FALSE result = ( s1 < s2); trace(result) ; // displays TRUE
11
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 false result = ( a && c); trace(result); //displays true result = ( a || b); trace(result); //displays true result = ( a || c); trace(result); //displays true result = !a; trace(result); //displays false result = !b; trace(result); //displays true
12
Operators - Bitwise Bitwise shift left << Bitwise shift right >> Bitwise AND & Bitwise OR | Bitwise Exclusive OR (XOR) ^ Bitwise inversion (NOT) ~
13
Operators – Bitwise - Examples var a:uint = 8; // Binary 00001000 var b:uint = 12;// Binary 00001100 var 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
14
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);
15
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 7 x += 5;// x now holds 12 x -= 3;// x now holds 9
16
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!”); }
17
AS3 program structure - MyTest2.as package { 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); }
18
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.
19
File locations Get organised before you start. Create a folder for this module. Create a subfolder named as3work
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.