Introduction to Object Oriented Programming with ActionScript 3.0

hihatcloverSoftware and s/w Development

Jul 4, 2012 (5 years and 4 months ago)

255 views

www.eLearningGuild.com
Introduction to Object
Oriented Programming
with ActionScript 3.0

Scott Hutton, MindCross Training
312

April 14-17, 2008
Orlando, FL
Page 1
Session 312 – Introduction to Object Oriented Programming with
ActionScript 3.0 – Scott Hutton, MindCross Training
MindCross Training & Consulting
Introduction to Object-Oriented
Programming with Action Script 3.0
Scott Hutton
MindCross Training
Target Audience
• Those familiar with ActionScript (AS) 2 (procedural
programming or using the timeline)
• Those new to object-oriented programming and AS3
• Those new to AS programming, but with experience in
other non-OO programming languages
2
April 14-17, 2008
Orlando, FL
Page 2
Session 312 – Introduction to Object Oriented Programming with
ActionScript 3.0 – Scott Hutton, MindCross Training
Goals and Objectives
The goals and objectives of this presentation are to:
• Understand object-oriented (OO) programming
terminology
• Gain the basic knowledge necessary to use OO
with AS 3
• Understand when to use procedural vs. OO programming
• Review and understand basic OOP examples in AS 3
3
Topics

Concepts of object-oriented programming

Procedural versus object-oriented paradigms

Understanding the terms

Working with classes

Encapsulation

Understanding inheritance and composition

Coding with polymorphism
4
April 14-17, 2008
Orlando, FL
Page 3
Session 312 – Introduction to Object Oriented Programming with
ActionScript 3.0 – Scott Hutton, MindCross Training
Object-Oriented Programming (OOP)

The concepts

A technique

Oriented around objects and data

ActionScript

AS 3 is object-oriented “capable”

Implementation of OO is optional
5
Procedural vs. Objected-Oriented

Two methods of programming

OOP is a way of organizing code –

Separating everything into objects

Procedural concentrates on data and program functionality
Procedural
programming
Scripting languages
Object-oriented
programming
Hides coding details into
the objects
Data
Functionality
Objects
6
April 14-17, 2008
Orlando, FL
Page 4
Session 312 – Introduction to Object Oriented Programming with
ActionScript 3.0 – Scott Hutton, MindCross Training
// Initialize screen
var currentPageGLB:Number = 1;
var lastPageGLB:Number = 5;
this.dtPageNumber.text = "Page " +currentPageGLB + " of " + lastPageGLB;
this.Prev.enabled = false;
this.Prev.visible = false;
this.mcMenu.visible = false;
var nextSlide:String;
var loader:Loader = new Loader();
var pageName:String = "flash/Slide1.swf";
var request:URLRequest = new URLRequest(pageName);
loader.load(request);
this.mcPlaceHolder.addChild(loader);
// --------------------------------------------------
// Define event listeners
Next.addEventListener(MouseEvent.CLICK, goToNext);
function goToNext(event_object:MouseEvent) {
currentPageGLB++;
nextSlide = "flash/Slide" + currentPageGLB + ".swf";
pageName = nextSlide;
//_root.mcClick.gotoAndPlay(2);
setPageInfo();
var request:URLRequest = new URLRequest(pageName);
loader.load(request);
this.mcPlaceHolder.addChild(loader);
}
// load Course menu
var xml:XML = <items>
<item label="Course Title" />
. . .
Procedural Programming with Flash
7
Flash file: TrainingShell.fla
Course: TrainingShell.swf
Object-Oriented Terminology

Classes

A template, blueprint

Encapsulation

Involves hiding the details of the class

Inheritance and Composition

Aids in the reuse of code

Polymorphism (overloading, overriding)

An entity that exists in multiple forms; an entity having
different behaviors

Provides flexibility
8
April 14-17, 2008
Orlando, FL
Page 5
Session 312 – Introduction to Object Oriented Programming with
ActionScript 3.0 – Scott Hutton, MindCross Training
What is a Class?
9

The foundation of object-based programming

A blueprint comprised of

Properties

Methods

Events

Methodology for coding

An object is an instance of a class

Promotes maintainability and reusability
Classes and Objects
10

The recording industry example
Recording industry and music
subscription services
Download
copy
Download copy
Download copy
Download
copy
Download
copy
April 14-17, 2008
Orlando, FL
Page 6
Session 312 – Introduction to Object Oriented Programming with
ActionScript 3.0 – Scott Hutton, MindCross Training
Classes and Objects in Flash
11

Flash includes classes (components) that you can use

Object instances are automatically declared in Flash
Drag the
component onto
the stage
Class properties
Instance
name
Classes and Objects in Flash
12

Flash includes classes (components) that you can use

When the component is not placed on the stage
// Instantiate the object
var myObject:Button = new Button();
myObject.label = "Hello world";
// Add to stage
addChild(myObject);
April 14-17, 2008
Orlando, FL
Page 7
Session 312 – Introduction to Object Oriented Programming with
ActionScript 3.0 – Scott Hutton, MindCross Training
Coding Classes in Flash
13

ActionScript file and Flash file in same directory
ActionScript file: Glossary.as
package {
public class Glossary {
public var TotalWords:int = 10;
public function WriteTotal() {
trace("Total words: "+ TotalWords);
}
}
}
Flash file: Example1.fla
var myGlossary:Glossary = new Glossary();
myGlossary.TotalWords = 10;
myGlossary.WriteTotal();
Directory: MindCrossExamples/Example1
Property: TotalWords
Method: WriteTotal
Coding Classes in Flash
14

ActionScript file and Flash file in sub-directories
ActionScript file: Glossary.as
package Classes {
public class Glossary {
public var TotalWords:int = 10;
public function WriteTotal() {
trace("Total words: "+ TotalWords);
}
}
}
Flash file: Example1.fla
import Classes.Glossary;
var myGlossary:Glossary = new Glossary();
myGlossary.TotalWords = 10;
myGlossary.WriteTotal();
Directory
MindCrossExamples
 Example1a
Classes
Directory
MindCrossExamples
 Example1a
April 14-17, 2008
Orlando, FL
Page 8
Session 312 – Introduction to Object Oriented Programming with
ActionScript 3.0 – Scott Hutton, MindCross Training
Coding Classes in Flash
15

Using Classpath to define the directory for classes
ActionScript file: Glossary.as
Package {
public class Glossary {
public var TotalWords:int = 10;
public function WriteTotal() {
trace("Total words: "+ TotalWords);
}
}
}
Flash file: Example1.fla
//no import statement required
var myGlossary:Glossary = new Glossary();
myGlossary.TotalWords = 10;
myGlossary.WriteTotal();
Directory
MindCrossExamples
Classes
Directory
MindCrossExamples
 Example1a
Encapsulation

Hides the details of the code and data

The developer decides what will be exposed to the users

Properties and methods can be public or private

Primary benefits

Modularity

Information hiding

Object design

What methods and properties
will be made public
16
April 14-17, 2008
Orlando, FL
Page 9
Session 312 – Introduction to Object Oriented Programming with
ActionScript 3.0 – Scott Hutton, MindCross Training
Encapsulation

Public properties and methods can be invoked by users

Flash example – Textfield class
...
private var _age:Number;
public function CalcAge()
{
...
}
Public methods
17
Inheritance

The ability to inherit properties and methods from
another class

Extends the functionality

Classes are organized into a tree structure

A subclass includes all the functionality of a higher level class

Everything from a higher level class is automatically applicable
to lower levels in the hierarchy

Class hierarchies

Classes can be created from other classes
18
April 14-17, 2008
Orlando, FL
Page 10
Session 312 – Introduction to Object Oriented Programming with
ActionScript 3.0 – Scott Hutton, MindCross Training

Tree structure showing relationships of Flash’s core
display object classes

Class: DisplayObject

Package: flash.display
Inheritance in Flash
19
Inheritance in Flash

MovieClip class
...
Public class MovieClip extends Sprite
...
...
Public class MyClass extends MovieClip
...
20
April 14-17, 2008
Orlando, FL
Page 11
Session 312 – Introduction to Object Oriented Programming with
ActionScript 3.0 – Scott Hutton, MindCross Training
Composition
21

Sometimes referred to as aggregation

An object composed of other objects

Contains an instance of a class
Composition
“Has a . . .”
Vehicle has a Tire
Inheritance
“Is a . . .”
RaceCar is a Vehicle
Composition
22
Composition
Inheritance
...
Public class RaceCar extends Vehicle
...
...
import tire;
Public class Vehicle{
{
...
var myObject:tire = new tire();
...
}
April 14-17, 2008
Orlando, FL
Page 12
Session 312 – Introduction to Object Oriented Programming with
ActionScript 3.0 – Scott Hutton, MindCross Training
Coding Inheritance in ActionScript
23

Any class can be extended
ActionScript file: Examle3.as
package {
import flash.display.MovieClip;
public class Example3 extends MovieClip
{
. . .
}
}
Flash file: Example3.fla
var myObject:Example3 = new Example3();
. . .
Directory: MindCrossExamples/Example3
Requirements: import
extends
Method Returns with AS 3
24
ActionScript file: Examle3.as
package {
import flash.display.MovieClip;
import flash.text.TextField;
public class WelcomeExample extends MovieClip
{
private var greetingMsg:String;
public function sayHello():String {
greetingMsg = "User: Hello world";
// get user information
return greetingMsg;
}
}
}
Directory: MindCrossExamples/WelcomExample
Set up return data
Flash file: WelcomeExample.fla
var myObject:WelcomeExample =
new WelcomeExample();
this.Greeting.text =
myObject.sayHello();
April 14-17, 2008
Orlando, FL
Page 13
Session 312 – Introduction to Object Oriented Programming with
ActionScript 3.0 – Scott Hutton, MindCross Training
Creating Properties in OOP
25
ActionScript file: Examle4.as
package {
public class Example4
{
private var PrivTotalPages:int;
public function get TotalPages():int
{
return PrivTotalPages
}
public function set
TotalPages(setValue:int):void
{
PrivTotalPages = setValue;
}
}
}
Directory: MindCrossExamples/WelcomExample
Create “getter” and “setter” function
...
var myObject:Example4 = new Example4();
myObject.TotalPages = 53;
PageNumber.text = "Page "+currentPage+
" of "+myObject.TotalPages;
Flash file: PropertyExample.fla
Polymorphism
26

The same method name may be implemented in different
classes

Example

x and y properties are referenced the same way regardless of
which class they are part of

Button

Sprite

MovieClip
April 14-17, 2008
Orlando, FL
Page 14
Session 312 – Introduction to Object Oriented Programming with
ActionScript 3.0 – Scott Hutton, MindCross Training
Polymorphism
27

The code behind the method for each class is unique
Shape
class
Circle
sub-class
Square
sub-class
Area method
Area method
Constructors
28

Methods used when an instance of a class is created

Required to have the same name as the class
package {
public class Example6
{
public function Example6()
{
trace("initiation code");
}
}
}
ActionScript file: Example6.as
Constructor method
...
var myObject:Example6 = new Example6();
...
Flash file: Constructors.fla
Constructors are automatically
invoked during instantiation
// Output - initiation code
April 14-17, 2008
Orlando, FL
Page 15
Session 312 – Introduction to Object Oriented Programming with
ActionScript 3.0 – Scott Hutton, MindCross Training
Summary

OOP is a programming style

Each class/object is responsible for a specific task

A class is a combination of data and operations

Encapsulation allows usage of the object, but, not
knowledge of its coding techniques and private code

An object is an instance of a class

The object’s functionality is dictated by the class from
which it was created

Classes are organized into a tree structure called
inheritance hierarchy

An object uses methods to provide functionality

An object uses properties to hold variable information
29