Introduction Actionscript 3.0
Introduction Actionscript 3.0
- Pallav Gala
Whats ActionScript?
ActionScript 2.0
- Introduced in 2003
- Provided class-based programming,
compile time type checking and cross
compilation to ActionScript 1.0 code
ActionScript 3.0
Introduced in 2006 with release of Adobe Flash
Player 9 and Adobe Flex 2.0
Executes up to 10 times faster than the legacy
ActionScript code on the new highly optimized
ActionScript Virtual Machine(AVM2)
Provide a true object oriented model for
programmers
Enables creation of applications with large data
sets and object oriented reusable code
Features of ActionScript 3.0
Constants
Data Types
Operators
Conditional Statements
Looping Statements
Variables and constants
A variable represent a piece of DATA to be used in the
program
Declaring and initializing a variable
Variable Name
Keyword var xyz:Number = 10;
Data Type
Examples: Examples:
- String: A text, or array of - MovieClip: a movie clip symbol
characters - SimpleButton: a button symbol
- Number: Any numeric value - Date: information about a single
- Int: Only integer values moment in time
- Boolean: True or False value - TextField: a dynamic or input text
field
Operators
Special functions that use one or more operands and return a
specific value
Examples:
- Arithmetic : +, -, *, /, %
- Logical : &, !, ^, |, &&, ||
- Relational : >, <, >=, <=, ==, !=
- Assignment : =, +=, -=
Conditional Looping
Used to execute certain Used to execute a block
statements based on the of code repeatedly, a
condition certain number of times
Example: Example:
- if..else - for
- If..else if - for..in
- switch - while
Object Oriented Programming in
ActionScript
OOP is basically a way to organize our code with the
help of objects
ActionScript 1.0 and 2.0 provided support for OOP,
but was limited
ActionScript 1.0 used Function Objects, to create
constructs similar to classes
ActionScript 2.0 introduced the concept of classes by
adding the keywords class and extends
ActionScript 3.0 extends over the previous versions
and provides better functionalities to developers
Classes(I)
The heart of all object-oriented code
development
An abstract representation of object
ActionScript classes have three types of
characteristics
public class Hello {
- Properties
public var string:String = "Hello World";
- Methods
public function Hello() {
- Events trace(string);
}
}
Classes(II)
Properties
- Like variables, represent one piece of data
- Example: public var string:String = "Hello World";
Defines a property string of the type String and initializes it to
Hello World
Methods
- Functions or behaviors that the object performs or exhibits
- Example:
Defines a method Hello(), which displays public function Hello() {
the content of string. trace(string);
This method is a constructor }
Classes(III)
Events
- Mechanisms that determine which statements need to executed
and when
- Events are things that occur and need to be responded to
- For example, a mouse click by user or a key typed in by user
- Following shows the ActionScript code to handle an event
function eventResponse(eventObject:EventType):void
{
// Actions performed in response to the event
}
eventSource.addEventListener(EventType.EVENT_NAME, eventResponse);
Class Object
For every class, ActionScript creates a special class object that allows
sharing of both behavior and state
Though it may not have practical implications for many applications
The block diagram below represents the structure of a class object, of a
class A
CA-Contains references to
other objects
TA-Stores the instance
properties
PA-Represents the class
object it was attached
through constructor
TCA-Represents static
properties defined by the
class
My First Program: Hello World
Action Window:
It is text box where
we can write our
ActionScript code
Timeline: Controls the timing that specifies when elements in the application appear
Since this is a simple program, it contains only 1 layer and 1 frame
Hello World
Hello.as file
Figure on the right shows
the definition of the class
Hello
A property string of type
String is defined and
using a
method(constructor) it is
displayed on the Output
OUPTUT
Window
On compiling the code,
we get the following
output on the OUTPUT
window
OOP Concepts
ActionScript 3.0 has more functionality, compared to
the previous versions, to support the OOP concepts
such as:
- Inheritance (final and override keywords added)
- Encapsulation (protected and internal keywords
added)
- Polymorphism
- Interfaces (IEventDispatcher interface added)
- Design Patterns (Factory, MVC and other patterns
included)
Advanced Topics
Core ActionScript Classes(I)
AS3 provides many in-built top-level classes that
facilitate the development of software applications
Working with dates and times:
- Classes: Date and Timer (in flash.utils package)
- Returns the current date and time, sets date and time,
etc. based on the number of parameters passed
- For example:
Core ActionScript Classes(II)
Working with strings:
- Classes: String
- Methods of this class can be used for string manipulations such
as compare, copy, concatenate, etc.
- For example:
- User Interactions
- Sound Files
- File system
- SQL Databases