166
Chapter 9: Creating Classes with ActionScript 2.0
Using class members: a simple example
One use of class (static) members is to maintain state information about a class and its instances.
For example, suppose you want to keep track of the number of instances that have been created
from a particular class. An easy way to do this is to use a class property that’s incremented each
time a new instance is created.
In the following example, you’ll create a class called Widget that defines a single, static instance
counter named
widgetCount
. Each time a new instance of the class is created, the value of
widgetCount
is incremented by 1 and the current value of
widgetCount
is displayed in the
Output panel.
To create an instance counter using a class variable:
1
Create a new ActionScript (AS) file.
2
Add the following code to the file:
class Widget {
static var widgetCount:Number = 0; // initialize class variable
function Widget() {
trace("Creating widget #" + widgetCount);
widg+;
}
}
The
widgetCount
variable is declared as static, and so initializes to 0 only once. Each time the
Widget class’s constructor function is called, it adds 1 to
widgetCount
, and then displays the
number of the current instance that’s being created.
3
Save your file as Widget.as.
4
Create a new Flash (FLA) document and save it as createWidget.fla in the same directory as
Widget.as.
In this file, you’ll create new instances of the Widget class.
5
In createWidget.fla, select Layer 1 in the Timeline and open the Actions panel (Window >
Development Panels > Actions).
6
Add the following code to the Actions panel.
// Before you create any instances of the class,
// widgetCount is zero (0)
trace("Widget count at start: " + Widget.widgetCount);
var widget_1 = new Widget();
var widget_2 = new Widget();
var widget_3 = new Widget();
7
Save the file, and then test it (Control > Test Movie).
You should see the following in the Output panel:
Widget count at start: 0
Creating widget # 0
Creating widget # 1
Creating widget # 2
Summary of Contents for FLASH MX 2004 - ACTIONSCRIPT
Page 1: ...ActionScript Reference Guide...
Page 8: ...8 Contents...
Page 12: ......
Page 24: ...24 Chapter 1 What s New in Flash MX 2004 ActionScript...
Page 54: ...54 Chapter 2 ActionScript Basics...
Page 80: ...80 Chapter 3 Writing and Debugging Scripts...
Page 82: ......
Page 110: ...110 Chapter 5 Creating Interaction with ActionScript...
Page 112: ......
Page 120: ...120 Chapter 6 Using the Built In Classes...
Page 176: ......
Page 192: ...192 Chapter 10 Working with External Data...
Page 202: ...202 Chapter 11 Working with External Media...
Page 204: ......
Page 782: ...782 Chapter 12 ActionScript Dictionary...
Page 793: ...Other keys 793 221 222 Key Key code...
Page 794: ...794 Appendix C Keyboard Keys and Key Code Values...
Page 798: ...798 Appendix D Writing Scripts for Earlier Versions of Flash Player...
Page 806: ...806 Appendix E Object Oriented Programming with ActionScript 1...
Page 816: ...816 Index...