|MySQL Conference and Expo April 14-17, 2008, Santa Clara, CA|
Introduction to OOP in Flash and ActionScript, Part 1
11. Now that we've added the code, let's test the movie. This time you should notice that the movie-clip object appears on the right side of the stage instead of the left. This is because the
12. For good measure, let's assign a few more values to a few other properties of the movie clip. Add two more lines of code just after the first:
13. When you test the movie now, you should see that the movie clip is somewhat transparent and has been scaled to 150% in the x direction.
Here's another experiment in which we'll utilize one of the methods of a movie-clip object.
Notice that in both of the preceding examples the properties and the method that were referenced affected only the object from which they were referenced. This is part of the design of OOP. Each object has its own set of properties and methods, and when they are referenced from that object, they affect only that object. For example, if you have two movie clips --
All movie clip instances are individual objects. Each of them shares a common set of properties, but each one has different values for those properties. And while all movie clips are capable of producing certain actions, they are not necessarily producing those same actions at the same time. This is akin to our orange example. Though oranges share common characteristics, they are all going to be different sizes with variations in color, flavor, etc. Likewise, not all oranges are growing at the same time. Nor are all oranges falling from the tree at the same time.
So how does an object like a movie clip know that it should have a certain set of properties and methods? If you look in the Actions toolbox or the Reference panel within Flash, you see that there are many different types of built-in objects, from the movie clip (
For example, movie clips have properties such as
Note: When we talk about built-in classes in Flash/ActionScript, this means that Macromedia has defined these classes, and they are available by default within the Flash authoring application and player.
The answer to our question as to how an object knows what set of properties and methods it should have is that all objects are associated with a prototype. In object-oriented terminology we call this prototype a class. Each of the types listed in the Actions toolbox is a built-in class.
So far, so good? This OOP thing is easier than you thought, right? Now that we've had a chance to examine how to work with movie-clip objects, let's also take a look at some of the other types of objects you can create in ActionScript.
In order to create a movie-clip object you dragged a movie-clip symbol from the library onto the stage and gave it a name. That was simple enough. But how about other types of objects? You cannot create an array symbol or an XML symbol and drag it onto the stage. So how do you go about creating these other types of objects?
Actually, movie clips, buttons, and text fields (all types of objects) are unique in how they are instantiated. The majority of objects are not represented visually on the stage. Instead, most objects are created programmatically using what are known as constructors. A constructor is a special function that shares its name with the class, and when used in a new statement (new is a keyword, a word with specific meaning in ActionScript) these constructors create a new instance of the class. The most fundamental type of object is derived from the
The preceding statement tells Flash to create a new object derived from the
All the built-in ActionScript classes are dynamic. That means you can add new, custom properties and methods to an instance in addition to getting and setting the properties and methods that are inherited from the class. In order to assign a new custom property you use dot syntax just as if you were assigning a value to an inherited property. If Flash cannot find the property within the object, it creates a new one.
Some constructors take parameters. For example, if you want to create a new instance of the
We've now taken an introductory look at basic object-oriented principles in ActionScript. I hope this article has helped clarify a few points for you. Obviously, this article only begins to touch on the depth of OOP. In the next installment of this article we'll discuss creating your own custom classes in ActionScript. Between now and then you may also want to consult my ActionScript Cookbook and/or ActionScript for Flash MX: The Definitive Guide for reference material on the built-in ActionScript classes. Additionally, you may find the following links to be useful references:
Joey Lott is a founding partner of The Morphic Group. He has written many books on Flex and Flash-related technologies, including Programming Flex 3, ActionScript 3 Cookbook, Adobe AIR in Action, and Advanced ActionScript 3 with Design Patterns.
O'Reilly & Associates recently released ActionScript Cookbook.
Return to the Web DevCenter.