What is Software? | What is Education? | What is Technology? | What is Research? | What is Wi-Fi? | What is Communication? | What is Customer Service?

Free SEO Tutorials & Help

Tutorials:   Adobe Flash   Adobe Photoshop   CorelDRAW   SEO  

Site Map

Website Development | Cable & Broadband Magazine | Solutions for Higher Education | Auction Anything Online

Adobe Flash Help & Tutorials

 Back to Topics

 

Using Object-Oriented Programming with ActionScript 1.0 in Flash

Object-Oriented Programming with ActionScript 1.0

Adding getter/setter properties to objects in ActionScript 1.0

About ActionScript 1.0

Using Function object properties in ActionScript 1.0

Assigning methods to a custom object in ActionScript 1.0

Invoking a function using the Function.call() method in ActionScript 1.0

Defining event handler methods in ActionScript 1.0

Specifying the object to which a function is applied using Function.apply() in ActionScript 1.0

Creating inheritance in ActionScript 1.0

 
Object-Oriented Programming with ActionScript 1.0

TOP

The information in this appendix comes from the Macromedia Flash MX documentation and provides information on using the ActionScript 1.0 object model to write scripts. It is included here for the following reasons:

  • If you want to write object-oriented scripts that support Flash Player 5, you must use ActionScript 1.0.

  • If you already use ActionScript 1.0 to write object-oriented scripts and aren't ready to switch to ActionScript 2.0, you can use this appendix to find or review information you need while writing your scripts.

If you have never used ActionScript to write object-oriented scripts and don't need to target Flash Player 5, you should not use the information in this appendix because writing object-oriented scripts using ActionScript 1.0 is deprecated. Instead, for information on using ActionScript 2.0, see Classes.

This chapter contains the following sections:

About ActionScript 1.0

Creating a custom object in ActionScript 1.0

Assigning methods to a custom object in ActionScript 1.0

Defining event handler methods in ActionScript 1.0

Creating inheritance in ActionScript 1.0

Adding getter/setter properties to objects in ActionScript 1.0

Using Function object properties in ActionScript 1.0

NOTE Some examples in this appendix use the Object.registerClass() method. This method is supported only in Flash Player 6 and later versions; don't use this method if you are targeting Flash Player 5.
 

About ActionScript 1.0

TOP

NOTE Many Flash users can greatly benefit from using ActionScript 2.0, especially with complex applications. For information on using ActionScript 2.0, see Classes.

ActionScript is an object-oriented programming language. Object-oriented programming uses objects, or data structures, to group together properties and methods that control the object's behavior or appearance. Objects let you organize and reuse code. After you define an object, you can refer to it by name without having to redefine it each time you use it.

A class is a generic category of objects. A class defines a series of objects that have common properties and can be controlled in the same ways. Properties are attributes that define an object, such as its size, position, color, transparency, and so on. Properties are defined for a class, and values for the properties are set for individual objects in the class. Methods are functions that can set or retrieve properties of an object. For example, you can define a method to calculate the size of an object. As with properties, methods are defined for an object class and then invoked for individual objects in the class.

ActionScript includes several built-in classes, including the MovieClip class, Sound class, and others. You can also create custom classes to define categories of objects for your applications.
Objects in ActionScript can be pure containers for data, or they can be graphically represented on the Stage as movie clips, buttons, or text fields. All movie clips are instances of the built-in MovieClip class, and all buttons are instances of the built-in Button class. Each movie clip instance contains all the properties (for example, _height, _rotation, _totalframes) and all the methods (for example, gotoAndPlay(), loadMovie(), startDrag()) of the MovieClip class.

To define a class, you create a special function called a constructor function. (Built-in classes have built-in constructor functions.) For example, if you want information about a bicycle rider in your application, you could create a constructor function, Biker(), with the properties time and distance and the method getSpeed(), which tells you how fast the biker is traveling:

function Biker(t, d) {

this.time = t;

this.distance = d;

this.getSpeed = function() {return this.time / this.distance;};

}

In this example, you create a function that needs two pieces of information, or parameters, to do its job: t and d. When you call the function to create new instances of the object, you pass it the parameters. The following code creates instances of the object Biker called emma and hamish, and it traces the speed of the emma instance, using the getSpeed() method from the previous ActionScript:

emma = new Biker(30, 5);

hamish = new Biker(40, 5);

trace(emma.getSpeed()); // traces 6

In object-oriented scripting, classes can receive properties and methods from each other according to a specific order, which is called inheritance. You can use inheritance to extend or redefine the properties and methods of a class. A class that inherits from another class is called a subclass. A class that passes properties and methods to another class is called a superclass.

A class can be both a subclass and a superclass.

An object is a complex data type containing zero or more properties and methods. Each property, like a variable, has a name and a value. Properties are attached to the object and contain values that can be changed and retrieved. These values can be of any data type: String, Number, Boolean, Object, MovieClip, or undefined. The following properties are of various data

types:

customer.name = "Jane Doe";

customer.age = 30;

customer.member = true;

customer.account.currentRecord = 609;

customer.mcInstanceName._visible = true;

The property of an object can also be an object. In line 4 of the previous example, account is a property of the object customer, and currentRecord is a property of the object account. The data type of the currentRecord property is Number.

Assigning methods to a custom object in ActionScript 1.0

TOP

NOTE Many Flash users can greatly benefit from using ActionScript 2.0, especially with complex applications. For information on using ActionScript 2.0, see Classes.

You can define the methods of an object inside the object's constructor function. However, this technique is not recommended because it defines the method every time you use the constructor function. The following example creates the methods getArea() and getDiameter(): and traces the area and diameter of the constructed instance myCircle with a radius set to 55:

function Circle(radius) {

this.radius = radius;

this.getArea = function(){

return Math.PI * this.radius * this.radius;

};

this.getDiameter = function() {

return 2 * this.radius;

};

}

var myCircle = new Circle(55);

trace(myCircle.getArea());

trace(myCircle.getDiameter());

Each constructor function has a prototype property that is created automatically when you define the function. The prototype property indicates the default property values for objects created

with that function. Each new instance of an object has a __proto__ property that refers to the prototype property of the constructor function that created it. Therefore, if you assign methods

to an object's prototype property, they are available to any newly created instance of that object. It's best to assign a method to the prototype property of the constructor function because it

exists in one place and is referenced by new instances of the object (or class). You can use the prototype and __proto__ properties to extend objects so that you can reuse code in an object

oriented manner. (For more information, see Creating inheritance in ActionScript 1.0.)

The following procedure shows how to assign an getArea() method to a custom Circle object.

To assign a method to a custom object:

  1. Define the constructor function Circle():

    function Circle(radius) {

    this.radius = radius;

    }

  2. Define the getArea() method of the Circle object. The getArea() method calculates the area of the circle. In the following example, you can use a function literal to define the getArea()

    method and assign the getArea property to the circle's prototype object:

    Circle.prototype.getArea = function () {

    return Math.PI * this.radius * this.radius;

    };

  3. The following example creates an instance of the Circle object:

    var myCircle = new Circle(4);

  4. Call the getArea() method of the new myCircle object using the following code:

    var myCircleArea = myCircle.getArea();

    trace(myCircleArea); // traces 50.265...

    ActionScript searches the myCircle object for the getArea() method. Because the object doesn't have a getArea() method, its prototype object Circle.prototype is searched for getArea().

    ActionScript finds it, calls it, and traces myCircleArea.

Defining event handler methods in ActionScript 1.0

TOP

NOTE Many Flash users can greatly benefit from using ActionScript 2.0, especially with complex applications. For information on using ActionScript 2.0, see Classes.

You can create an ActionScript class for movie clips and define the event handler methods in the prototype object of that new class. Defining the methods in the prototype object makes all the instances of this symbol respond the same way to these events.

You can also add an onClipEvent() or on() event handler methods to an individual instance to provide unique instructions that run only when that instance's event occurs. The onClipEvent() and on() methods don't override the event handler method; both events cause their scripts to run. However, if you define the event handler methods in the prototype object and also define an event handler method for a specific instance, the instance definition overrides the prototype definition.

To define an event handler method in an object's prototype object:

  1. Create a movie clip symbol and set the linkage identifier to theID by selecting the symbol in the Library panel and selecting Linkage from the Library pop-up menu.

  2. In the Actions panel (Window > Actions), use the function statement to define a new class, as shown in the following example:

    // define a class

    function myClipClass() {}

    This new class is assigned to all instances of the movie clip that are added to the application by the timeline or that are added to the application with the attachMovie() or duplicateMovieClip()

    method. If you want these movie clips to have access to the methods and properties of the built-in MovieClip object, you need to make the new class inherit from the MovieClip class.

  3. Enter code, such as the following example:

    // inherit from MovieClip class

    myClipClass.prototype = new MovieClip();

    Now, the class myClipClass inherits all the properties and methods of the MovieClip class.

  4. Enter code, such as the following example, to define the event handler methods for the new class:

    // define event handler methods for myClipClass class

    myClipClass.prototype.onLoad = function() {trace("movie clip loaded");}

    myClipClass.prototype.onEnterFrame = function() {trace("movie clip entered frame");}

  5. Select Window > Library to open the Library panel if it isn't already open.

  6. Select the symbols that you want to associate with your new class, and select Linkage from the Library panel pop-up menu.

  7. In the Linkage Properties dialog box, select Export for ActionScript.

  8. Enter a linkage identifier in the Identifier text box.

    The linkage identifier must be the same for all symbols that you want to associate with the new class. In the myClipClass example, the identifier is theID.

  9. Enter code, such as the following example, in the Actions panel:

    // register class

    Object.registerClass("theID", myClipClass);

    this.attachMovie("theID","myName",1);

    This step registers the symbol whose linkage identifier is theID with the class myClipClass. All instances of myClipClass have event handler methods that behave as defined in step 4. They also behave the same as all instances of the MovieClip class because you told the new class to inherit from the class MovieClip in step 3.

    The complete code is shown in the following example:

    function myClipClass(){}

    myClipClass.prototype = new MovieClip();

    myClipClass.prototype.onLoad = function(){

    trace("movie clip loaded");

    }

    myClipClass.prototype.onPress = function(){

    trace("pressed");

    }

    myClipClass.prototype.onEnterFrame = function(){

    trace("movie clip entered frame");

    }

    myClipClass.prototype.myfunction = function(){

    trace("myfunction called");

    }

    Object.registerClass("myclipID",myClipClass);

    this.attachMovie("myclipID","clipName",3);

Creating inheritance in ActionScript 1.0

TOP

NOTE Many Flash users can greatly benefit from using ActionScript 2.0, especially with complex applications. For information on using ActionScript 2.0, see Classes. Inheritance is a means of organizing, extending, and reusing functionality. Subclasses inherit properties and methods from superclasses, and add their own specialized properties and methods. For example, reflecting the real world, Bike would be a superclass and MountainBike and Tricycle would be subclasses of the superclass. Both subclasses contain, or inherit, the methods and properties of the superclass (for example, wheels). Each subclass also has its own properties and methods that extend the superclass (for example, the MountainBike subclass would have a gears property).

You can use the elements prototype and __proto__ to create inheritance in ActionScript.

All constructor functions have a prototype property that is created automatically when the function is defined. The prototype property indicates the default property values for objects created with that function. You can use the prototype property to assign properties and methods to a class. (For more information, see Assigning methods to a custom object in ActionScript 1.0.)

All instances of a class have a __proto__ property that tells you the object from which they inherit. When you use a constructor function to create an object, the __proto__ property is set to refer to the prototype property of its constructor function.

Inheritance proceeds according to a definite hierarchy. When you call an object's property or method, ActionScript looks at the object to see if such an element exists. If it doesn't exist,

ActionScript looks at the object's __proto__ property for the information (myObject.__proto__). If the property is not a property of the object's __proto__ object, ActionScript looks at myObject.__proto__.__proto__, and so on.

The following example defines the constructor function Bike():

function Bike(length, color) {

this.length = length;

this.color = color;

this.pos = 0;

}

The following code adds the roll() method to the Bike class:

Bike.prototype.roll = function() {return this.pos += 20;};

Then, you can trace the position of the bike with the following code:

var myBike = new Bike(55, "blue");

trace(myBike.roll()); // traces 20.

trace(myBike.roll()); // traces 40.

Instead of adding roll() to the MountainBike class and the Tricycle class, you can create the MountainBike class with Bike as its superclass, as shown in the following example:

MountainBike.prototype = new Bike();

Now you can call the roll() method of MountainBike, as shown in the following example:

var myKona = new MountainBike(20, "teal");

trace(myKona.roll()); // traces 20

Movie clips do not inherit from each other. To create inheritance with movie clips, you can use Object.registerClass() to assign a class other than the MovieClip class to movie clips.

Adding getter/setter properties to objects in ActionScript 1.0

TOP

NOTE Many Flash users can greatly benefit from using ActionScript 2.0, especially with complex applications. For information on using ActionScript 2.0, see Classes.

You can create getter/setter properties for an object using the Object.addProperty() method.

A getter function is a function with no parameters. Its return value can be of any type. Its type can change between invocations. The return value is treated as the current value of the property.

A setter function is a function that takes one parameter, which is the new value of the property. For instance, if property x is assigned by the statement x = 1, the setter function is passed the parameter 1 of type Number. The return value of the setter function is ignored.

When Flash reads a getter/setter property, it invokes the getter function, and the function's return value becomes a value of prop. When Flash writes a getter/setter property, it invokes the setter function and passes it the new value as a parameter. If a property with the given name already exists, the new property overwrites it.

You can add getter/setter properties to prototype objects. If you add a getter/setter property to a prototype object, all object instances that inherit the prototype object inherit the getter/setter property. You can add a getter/setter property in one location, the prototype object, and have it propagate to all instances of a class (similar to adding methods to prototype objects). If a getter/setter function is invoked for a getter/setter property in an inherited prototype object, the reference passed to the getter/setter function is the originally referenced object, not the prototype object.

The Debug > List Variables command in test mode supports getter/setter properties that you add to objects using Object.addProperty(). Properties that you add to an object in this way appear with other properties of the object in the Output panel. Getter/setter properties are identified in the Output panel with the prefix [getter/setter]. For more information on the List Variables command, see Using the Output panel.

Using Function object properties in ActionScript 1.0

TOP

NOTE Many Flash users can greatly benefit from using ActionScript 2.0, especially with complex applications. For information on using ActionScript 2.0, see Classes.

You can specify the object to which a function is applied and the parameter values that are passed to the function, using the call() and apply() methods of the Function object. Every function in ActionScript is represented by a Function object, so all functions support call() and apply(). When you create a custom class using a constructor function, or when you define methods for a custom class using a function, you can invoke call() and apply() for the function.

Invoking a function using the Function.call() method in ActionScript 1.0

TOP

NOTE Many Flash users can greatly benefit from using ActionScript 2.0, especially with complex applications. For information on using ActionScript 2.0, see Classes.

The Function.call() method invokes the function represented by a Function object.

In almost all cases, the function call operator (()) can be used instead of the call() method. The function call operator creates code that is concise and readable. The call() method is primarily useful when the this parameter of the function invocation needs to be explicitly controlled. Normally, if a function is invoked as a method of an object, within the body of the function, this is set to myObject, as shown in the following example:

myObject.myMethod(1, 2, 3);

In some situations, you might want this to point somewhere else; for instance, if a function must be invoked as a method of an object but is not actually stored as a method of that object, as shown in the following example:

myObject.myMethod.call(myOtherObject, 1, 2, 3);

You can pass the value null for the thisObject parameter to invoke a function as a regular function and not as a method of an object. For example, the following function invocations are equivalent:

Math.sin(Math.PI / 4)

Math.sin.call(null, Math.PI / 4)

To invoke a function using the Function.call() method:

  • Use the following syntax:

    myFunction.call(thisObject, parameter1, ..., parameterN)

    The method takes the following parameters:

  • The parameter thisObject specifies the value of this within the function body.

  • The parameters parameter1..., parameterN specify parameters to be passed to myFunction. You can specify zero or more parameters.

Specifying the object to which a function is applied using Function.apply() in  ActionScript 1.0

TOP

NOTE Many Flash users can greatly benefit from using ActionScript 2.0, especially with complex applications. For information on using ActionScript 2.0, see Classes.

The Function.call() method invokes the function represented by a Function object.

In almost all cases, the function call operator (()) can be used instead of the call() method. The function call operator creates code that is concise and readable. The call() method is primarily useful when the this parameter of the function invocation needs to be explicitly controlled. Normally, if a function is invoked as a method of an object, within the body of the function, this is

set to myObject, as shown in the following example:

myObject.myMethod(1, 2, 3);

In some situations, you might want this to point somewhere else; for instance, if a function must be invoked as a method of an object but is not actually stored as a method of that object, as shown in the following example:

myObject.myMethod.call(myOtherObject, 1, 2, 3);

You can pass the value null for the thisObject parameter to invoke a function as a regular function and not as a method of an object. For example, the following function invocations are equivalent:

Math.sin(Math.PI / 4)

Math.sin.call(null, Math.PI / 4)

To invoke a function using the Function.call() method:

  • Use the following syntax:

    myFunction.call(thisObject, parameter1, ..., parameterN)

  • The method takes the following parameters:

  • The parameter thisObject specifies the value of this within the function body.

  • The parameters parameter1..., parameterN specify parameters to be passed to myFunction. You can specify zero or more parameters.

Copyright ADOBE - All Rights Reserved Worldwide

 

 

More Topics:

Working with Flash Documents

How to work in Flash WorkSpace

Working with Projects in Flash

Process to Build your First Application in Flash

Using Symbols, Instances and Library Assets in Flash

How to Build Video Player in Flash

How to Work with Color, Strokes and Fills in Flash

How to Create Document in Flash

What is Vector and Bitmap Graphics in Flash

How to Create a Banner in Flash, Part 1

How to Work with Text in Flash

How to Create a Banner in Flash, Part 2

How to Use Imported Artwork in Flash

How to Create a Banner in Flash, Part 3

How to Work with Graphic Objects in Flash

How to Work with Layers in Flash

How to Use Filters and Blends

Working with Graphics in Flash

What is Accessibility Features in Flash

How to Create Motion (Shape Tween & Motion Tween) in Flash

How to Create an Application in Flash

What is Masking in Flash

How to Work with Video in Flash

How to Use Layout Tools in Flash

What are Behaviors in Flash

How to Work with Sound in Flash

How to Create Symbols and Instances in Flash

What is ActionScript in Flash

How to Write ActionScript With Script Assist in Flash

How to Add Button Animation and Navigation in Flash

What is Data Integration in Flash

How to Work with Screens

How to Create a Presentation with Screens

What is Extending Flash

How to Create Multilanguage Text in Flash

How to Create Graphics: Draw in Flash

What is Flash Lite

Ways of Data Integration

How to Create Graphics: Create a Timeline Animation in Flash

Getting Started with Flash Lite in Flash

How to Publish Flash Documents

How to Create Graphics: Making Animations with Easing

Learning Flash Lite 1.X ActionScript in Flash

How to Export Flash Content and Images from Flash

How to Create Graphics: Applying Gradients in Flash

Process of Writing and Editing ActionScript 2.0 in Flash

How to Create Accessible Content in Flash

How to Create Graphics: Apply Graphic Filters and Blends

What is Data and Data Types in Flash

Process of Printing from SWF Files in Flash

Using ActionScript: How to Use Script Assist mode in Flash

Learn Syntax and Language Fundamentals in Flash

How to Create E-learning Content in Flash

Using ActionScript: How to Write Scripts in Flash

Working with Functions and Methods in Flash

Process of Using Templates in Flash

Using ActionScript: Process of Adding Interactivity in Flash

What are Classes in Flash

Control Tag Summary of XML to UI in Flash

Using ActionScript: How to Create a Form with Conditional Logic and Send Data in Flash

What is Inheritance in Flash

What is Data Integration: Overview

Using ActionScript: How to Work with Objects and Classes in Flash

Overview on Interfaces in Flash

What is Data Integration: Using XML for a Timesheet

How to Work with Text and Strings in Flash

How to use Handling Events in Flash

What is Data Integration: Using XUpdate to Update the Timesheet

Learning Animation, Filters and Drawings in Flash

How to Work with Movie Clips in Flash

How to Create Interaction with ActionScript in Flash

How to Work with Images, Sound, and Video in Flash

How to Work with External Data in Flash

What is Security in Flash

How to Debug Applications in Flash

List of Error Messages in Flash

Using Object-Oriented Programming with ActionScript 1.0 in Flash

How to Write Scripts for Earlier Versions of Flash Player in Flash

List of all Keyboard Keys and Key Code Values for using in Flash

Terminology

Introduction to Components in Flash

What are Components in Flash

How to Create an Application with Components

How to Work with Components in Flash

How to Handle Component Events in Flash

How to Customize Components in Flash

How to Create Components in Flash

What is Collection Properties in Flash