Flash
  Home arrow Flash arrow Page 2 - Object-oriented ActionScript
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
FLASH

Object-oriented ActionScript
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2004-08-30

    Table of Contents:
  • Object-oriented ActionScript
  • Key Object-Oriented Programming Concepts
  • Object Creation and Usage
  • Encapsulation and Datatypes
  • Inheritance, Packages, Compilation
  • Starting an Objected-Oriented Application
  • An Example Website

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Object-oriented ActionScript - Key Object-Oriented Programming Concepts


    (Page 2 of 7 )

    An object is a self-contained software module that contains related functions (called its methods) and variables (called its properties). Individual objects are created from classes, which provide the blueprint for an object’s methods and properties. That is, a class is the template from which an object is made. Classes can represent theoretical concepts, such as a timer, or physical entities in a program, such as a pull-down menu or a spaceship. A single class can be used to generate any number of objects, each with the same general structure, somewhat as a single recipe can be used to bake any number of muffins. For example, an OOP space fighting game might have 20 individual SpaceShip objects on screen at one time, all created from a single SpaceShip class. Similarly, the game might have one 2dVector class that represents a mathematical vector but thousands of 2dVector objects in the game.

    Note: The term instance is often used as a synonym for object. For example, the phrases “Make a new SpaceShip instance” and “Make a new SpaceShip object” mean the same thing. Creating a new object from a class is sometimes called instantiation. To build an object-oriented program, we:

    1. Create one or more classes.

    2. Make (i.e., instantiate) objects from those classes.

    3. Tell the objects what to do.

    What the objects do determines the behavior of the program.

    In addition to using the classes we create, a program can use any of the classes built into the Flash Player. For example, a program can use the built-in Sound class to create Sound objects. An individual Sound object represents and controls a single sound or a group of sounds. Its setVolume( ) method can raise or lower the volume of a sound. Its loadSound( ) method can retrieve and play an MP3 sound file. And its duration property can tell us the length of the loaded sound, in milliseconds. Together, the built-in classes and our custom classes form the basic building blocks of all OOP applications in Flash.

    Class Syntax

    Let’s jump right into a tangible example. Earlier, I suggested that a space fighting game would have a SpaceShip class. The ActionScript that defines the class might looklike the source code shown in Example 2-1 (don’t worry if much of this code is new to you; we’ll study it in detail in the coming chapters).

    Example 2-1. The SpaceShip class

    class SpaceShip {
     // This is a public property named speed.
     public var speed:Number;
     // This is a private property named damage.
     private var damage:Number;
     // This is a constructor function, which initializes
     // each SpaceShip instance.
     public function SpaceShip ( ) {
     speed = 100;
     damage = 0;
     }
     // This is a public method named fireMissile( ).
     public function fireMissile ( ):Void {
     // Code that fires a missile goes here.
     }
     // This is a public method named thrust( ).
     public function thrust ( ):Void {
     // Code that propels the ship goes here.
     }
    }

    Notice how the SpaceShip class groups related aspects of the program neatly together (as do all classes). Variables (properties), such as speed and damage, related to spaceships are grouped with functions (methods) used to move a spaceship and fire its weapons. Other aspects of the program, such as keeping score and drawing the background graphics can be kept separate, in their own classes (not shown in this example).

    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.

    More Flash Articles
    More By O'Reilly Media


       · This is an excellent article. It has not only given me a much needed and easy...
       · I noted that the code for the Tomagotchi exercise and introduction to...
     

    FLASH ARTICLES

    - Using XML and ActionScript with Flex Applica...
    - Interfaces and Events with ActionScript and ...
    - Manipulating Data with ActionScript in Flex ...
    - ActionScript Syntax for Flex Applications
    - ActionScript in Flex Applications
    - A Closer Look at Apollo`s File System API
    - Using the File System API
    - ActionScript 101
    - Flash Buttons
    - Advanced Flash Animation
    - Creating Your First Animated Movie with Flas...
    - Flash: Building Blocks
    - Building Preloaders
    - Fun Things to Do with Movie Clips in Flash MX
    - Referencing Movie Clips in Flash MX







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway