Flash Objects


The trivia game that we will build by the end of this book is complex and sophisticated. It starts, however, with some simple ideas that will be recognizable in each stage of development. Each round of the game is presented in Q&A format: a single Q and multiple choice As. The quizzes are stored in memory as an array of objects.

Predefined Objects

ActionScript comes with several well-defined built-in objects. The XML object will absorb much of our attention. It has methods (like toString ()) and properties (like loaded ). It also has a constructor, new XML() . This is the method called when the object is created to set up a new instance of the object. A constructor may load default values into variables , position objects, and take arguments that help individualize and define a particular instance. XML, like all provided classes, is also extensible.

Other Flash objects include traditional data types (such as Number, String, Array, and the generic Object ) and system constructs (like Date or XMLSocket ). Flash also supplies multimedia objects ( Color, Sound, Mouse ). Finally it includes the object MovieClip, which describes the methods these animations can perform (like play() or gotoAndStop() ) and the properties they all have (like _visible or _xscale ).

Code Objects

ActionScript programmers can extend the palette of objects they are given, as in any object- oriented language. The procedure for defining a new object is simple.

To define an object, first a function is written and given the name of the object. This function will be the object's constructor, the new operator that instantiates the object.

ActionScript
 function Question( stuff ) {  . . . startup code for Question. . . . } 

Then the _prototype property allows us to add methods and properties to our object. It is the object template. The templated definitions

ActionScript
 Question._prototype.ask = function () {  . . . method code. . . .} 

become specific and real when application code is encountered :

ActionScript
 tuffy= new Question( tuffStuff ); tuffy.ask(); 

MovieClip Objects

Every time we design a MovieClip (the basic unit of Flash Animation), we are essentially making an object. Though they are not always identified as objects, MovieClips have the same qualities as code objects and ought to be considered at the same time. Like objects, they can be prototyped and instantiated , and they have a private name space.

MovieClips have predefined methods and properties. The predefined properties all begin with an underscore : position as _xpos and _ypos , size as _xscale , _yscale , and so on. The methods include play() and stop() .

When we create new MovieClips, we frequently give them their own variables and their own functions. These are addressed and used exactly like the built-in properties and methods of MovieClips.

Symbols and Instances

The object definition of a MovieClip is created while it is open in the editing window. What might be called a class or a template elsewhere is here called a symbol. The symbol libraries have plenty of object definitions, both predefined and custom-built. When we place a MovieClip on the stage ”inside another MovieClip (or in the Scene) ”we have created an instance of the object.

Functions and variables are usually defined when the symbol is created, but they are usually accessed by individual instances: The basic definition of a MovieClip includes scale properties and play methods. But it is an individual instance that is sized or that plays.

We will see the same characteristics in the objects we build ”both in our own specialized MovieClips and in our original ActionScript objects.

Note

Instantiation

Sometimes the concept of instantiation can be hard to keep clear. An analogy might be a television program. A master definition is created (at the studio). An infinite number of instances of this show can exist (in living rooms) without diminishing the original. Each instance can be different (larger, smaller, louder, brighter, bluer) than it appeared at the studio. These differences result from adjustments made to the instance (tweaking the volume, for example). Adjustments to the instance do not affect the original definition, nor do they alter other instances.

MovieClip objects behave the same way. You may draw a robot 300 pixels high, but in one scene he might only be 50 pixels high. In fact, he may appear several times in the same scene at different sizes. Each is an independent instance.

ActionScript objects behave the same way ”the contsructor and prototype define generic behavior, but the values within their variables create individual identity for the instances.




Flash and XML[c] A Developer[ap]s Guide
Flash and XML[c] A Developer[ap]s Guide
ISBN: 201729202
EAN: N/A
Year: 2005
Pages: 160

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net