Anda di halaman 1dari 16

www.eLearningGuild.

com

Introduction to Object Oriented Programming with ActionScript 3.0


Scott Hutton, MindCross Training

312

April 14-17, 2008 Orlando, FL

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

Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training

Page 1

April 14-17, 2008 Orlando, FL

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

Topics

4

Concepts of object-oriented programming Procedural versus object-oriented paradigms Understanding the terms Working with classes Encapsulation Understanding inheritance and composition Coding with polymorphism

Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training

Page 2

April 14-17, 2008 Orlando, FL

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

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


Data Functionality Objects

Procedural programming Scripting languages


6

Object-oriented programming Hides coding details into the objects

Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training

Page 3

April 14-17, 2008 Orlando, FL

Procedural Programming with Flash


Flash file: TrainingShell.fla
// 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" />

Course: TrainingShell.swf

. . .

Object-Oriented Terminology

Classes

A template, blueprint Involves hiding the details of the class Aids in the reuse of code An entity that exists in multiple forms; an entity having different behaviors Provides flexibility

Encapsulation

Inheritance and Composition


Polymorphism (overloading, overriding)


Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training

Page 4

April 14-17, 2008 Orlando, FL

What is a Class?

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


9

Classes and Objects


The recording industry example

Download copy

Download copy

Recording industry and music subscription services

Download copy Download copy Download copy

10

Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training

Page 5

April 14-17, 2008 Orlando, FL

Classes and Objects in Flash


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

11

Classes and Objects in Flash


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);

12

Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training

Page 6

April 14-17, 2008 Orlando, FL

Coding Classes in Flash


ActionScript file and Flash file in same directory


ActionScript file: Glossary.as

Directory: MindCrossExamples/Example1

package { public class Glossary {

Property: TotalWords

public var TotalWords:int = 10; public function WriteTotal() { trace("Total words: "+ TotalWords); } } }
Flash file: Example1.fla

Method: WriteTotal

var myGlossary:Glossary = new Glossary(); myGlossary.TotalWords = 10; myGlossary.WriteTotal();

13

Coding Classes in Flash


ActionScript file and Flash file in sub-directories


Directory

ActionScript file: Glossary.as

package Classes { public class Glossary { public var TotalWords:int = 10; public function WriteTotal() { trace("Total words: "+ TotalWords); } } }

MindCrossExamples Example1a Classes

Flash file: Example1.fla

import Classes.Glossary; var myGlossary:Glossary = new Glossary(); myGlossary.TotalWords = 10; myGlossary.WriteTotal();

Directory

MindCrossExamples Example1a

14

Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training

Page 7

April 14-17, 2008 Orlando, FL

Coding Classes in Flash


Using Classpath to define the directory for classes


Directory

ActionScript file: Glossary.as

Package { public class Glossary { public var TotalWords:int = 10; public function WriteTotal() { trace("Total words: "+ TotalWords); } } }

MindCrossExamples Classes

Flash file: Example1.fla

//no import statement required var myGlossary:Glossary = new Glossary(); myGlossary.TotalWords = 10; myGlossary.WriteTotal();

Directory

MindCrossExamples Example1a

15

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 What methods and properties will be made public

Object design

16

Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training

Page 8

April 14-17, 2008 Orlando, FL

Encapsulation

Public properties and methods can be invoked by users


... private var _age:Number; public function CalcAge() { ... }

Flash example Textfield class


Public methods

17

Inheritance

The ability to inherit properties and methods from another class


Extends the functionality 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 Classes can be created from other classes

Classes are organized into a tree structure


Class hierarchies

18

Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training

Page 9

April 14-17, 2008 Orlando, FL

Inheritance in Flash

Tree structure showing relationships of Flashs core display object classes


Class: DisplayObject Package: flash.display

19

Inheritance in Flash

MovieClip class

... ... Public class MovieClip extends Sprite ... 20 Public class MyClass extends MovieClip ...

Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training

Page 10

April 14-17, 2008 Orlando, FL

Composition

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

21

Composition
Composition
... import tire; Public class Vehicle{ { ... var myObject:tire = new tire(); ... }

Inheritance
... Public class RaceCar extends Vehicle ...

22

Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training

Page 11

April 14-17, 2008 Orlando, FL

Coding Inheritance in ActionScript


Any class can be extended


ActionScript file: Examle3.as

Directory: MindCrossExamples/Example3

package { import flash.display.MovieClip; public class Example3 extends MovieClip { . . . } }

Requirements: import extends

Flash file: Example3.fla

var myObject:Example3 = new Example3(); . . .

23

Method Returns with AS 3


Directory: MindCrossExamples/WelcomExample
ActionScript file: Examle3.as

Set up return data

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; } } } this.Greeting.text = myObject.sayHello();

Flash file: WelcomeExample.fla

var myObject:WelcomeExample = new WelcomeExample();

24

Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training

Page 12

April 14-17, 2008 Orlando, FL

Creating Properties in OOP


Directory: MindCrossExamples/WelcomExample
ActionScript file: Examle4.as

Create getter and setter function

package { public class Example4 { private var PrivTotalPages:int; public function get TotalPages():int { return PrivTotalPages } public function set TotalPages(setValue:int):void { Flash file: PropertyExample.fla PrivTotalPages = setValue; ... } var myObject:Example4 = new Example4(); } myObject.TotalPages = 53; } PageNumber.text = "Page "+currentPage+ " of "+myObject.TotalPages;

25

Polymorphism

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

26

Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training

Page 13

April 14-17, 2008 Orlando, FL

Polymorphism

The code behind the method for each class is unique


Shape class

Circle sub-class

Square sub-class

Area method

Area method

27

Constructors

Methods used when an instance of a class is created Required to have the same name as the class

ActionScript file: Example6.as

package { public class Example6 { public function Example6() { trace("initiation code"); } } }


Flash file: Constructors.fla

Constructor method

... Constructors are automatically invoked during instantiation // Output - initiation code var myObject:Example6 = new Example6(); ...

28

Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training

Page 14

April 14-17, 2008 Orlando, FL

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 objects 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

Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training

Page 15

Anda mungkin juga menyukai