Home arrow Flash arrow Page 3 - Object-oriented ActionScript
FLASH

Object-oriented ActionScript


This chapter provides a high-level overview of object-oriented programming (OOP) in ActionScript for experienced programmers who are making their first foray into Flash development. (From the book Essential ActionScript 2.0, by Colin Moock, O'Reilly Media, ISBN:0596006527.)

Author Info:
By: O'Reilly Media
Rating: 4 stars4 stars4 stars4 stars4 stars / 15
August 30, 2004
TABLE OF CONTENTS:
  1. · Object-oriented ActionScript
  2. · Key Object-Oriented Programming Concepts
  3. · Object Creation and Usage
  4. · Encapsulation and Datatypes
  5. · Inheritance, Packages, Compilation
  6. · Starting an Objected-Oriented Application
  7. · An Example Website

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Object-oriented ActionScript - Object Creation and Usage
(Page 3 of 7 )

Objects are created (instantiated) with the new operator, as in:

new ClassName()

where ClassName is the name of the class from which the object will be created.

For example, when we want to create a new SpaceShip object in our hypothetical game, we use this code:

new SpaceShip()

Note: The syntax for creating objects (e.g., new SpaceShip( )) is the same in ActionScript 2.0 as it was in ActionScript 1.0. However, the syntax for defining classes in ActionScript 2.0 differs from ActionScript 1.0.

Most objects are stored somewhere after they’re created so that they can be used later in the program. For example, we might store a SpaceShip instance in a variable named ship, like this:

var ship:SpaceShip = new SpaceShip();

Each object is a discrete data value that can be stored in a variable, an array element, or even a property of another object. For example, if you create 20 alien spaceships, you would ordinarily store references to the 20 SpaceShip objects in a single array. This allows you to easily manipulate multiple objects by cycling through the array and, say, invoking a method of the SpaceShip class on each object.

Object Usage

An object’s methods provide its capabilities (i.e., behaviors)—things like “fire missile,” “move,” and “scroll down.” An object’s properties store its data, which describes its state at any given point in time. For example, at a particular point in a game, our ship’s current state might be speed is 300, damage is 14.

Methods and properties that are defined as public by an object’s class can be accessed from anywhere in a program. By contrast, methods and properties defined as private can be used only within the source code of the class or its subclasses. As we’ll learn in Chapter 4, methods and properties should be defined as public only if they must be accessed externally.

To invoke a method, we use the dot operator (i.e., a period) and the function call operator (i.e., parentheses). For example:

// Invoke the ship object's fireMissile( ) method.
ship.fireMissile();

To set a property, we use the dot operator and an equals sign. For example:

// Set the ship's speed property to 120.
ship.speed = 120;

To retrieve a property’s value, we use the dot operator on its own. For example:

// Display the value of the speed property in the Output panel.
trace(ship.speed);

Buy the book!If you've enjoyed what you've seen here, or to get more information, click on the "Buy the book!" graphic. Pick up a copy today!

Visit the O'Reilly Network http://www.oreillynet.com for more online content.


blog comments powered by Disqus
FLASH ARTICLES

- More Top Flash Game Tutorials
- Top Flash Game Tutorials
- Best Flash Photo Gallery Tutorials
- The Top Flash Tutorials for Menus
- 7 Great Flash Tutorials
- Adobe Creative Suite 5.5 Now Available
- Critical Flash Vulnerability Heats Up the Web
- More on Nonpersistent Client-Side Remote Sha...
- Nonpersistent Client-Side Remote Shared Obje...
- Using the Decorator Pattern for a Real Web S...
- Using Concrete Decorator Classes
- Delving More Deeply into the Decorator Patte...
- The Decorator Pattern in Action
- A Simple Decorator Pattern Example
- Decorator Pattern

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials