- What Is ActionScript?
- What ActionScript Can and Can't Do
- Variables
- Objects and Object-Oriented Scripting
- Object and Frame Actions
- Dot Syntax
- Properties
- Methods
- Events and Event Handlers
- Functions
- Conclusion
Objects and Object-Oriented Scripting
Both ActionScript and JavaScript are called object-oriented scripting languages. Let's go over what this means, since it's an odd concept if you haven't been exposed to it before.
Scripting languages organize information into groups called classes. You can create multiple instances of these classes, which are called objects.
Classes and objects are just ways to group together chunks of information. To use a real-world analogy, you could create a "bicycle" class. That bicycle has lots of different properties to it, such as how many gears there are, how big the tires are, and what color it is. You can also perform some actions with the bike: You can, among other things, pedal, brake, and turn (these are called methods).
In Flash MX, all movie clips are objects (also called instances) of the class MovieClip. Since all movie clips are objects of the same class, they all have the same properties and the same methods (that'll make sense soonhang in there!).
Buttons are also objects. While there are some significant differences between buttons and movie clips, which we'll cover later, I consider itas do many other Flash folkuseful to think of buttons as a kind of movie clip.
Flash has a number of predefined objects you can access: Accessibility, Arguments, Array, Boolean, Button, Capabilities, Color, Components, CustomActions, Date, FStyleFormat, Key, LoadVars, Math, Mouse, MovieClip, Number, Object, Selection, Sound, Stage, System, String, Textfield, TextFormat, XML, and XMLSocket. If this is an overwhelming list now, don't worrywe'll spend much of this book looking at them. Those of you coming from Flash 5 will notice Macromedia has incorporated about twice as many predefined objects in Flash MX. This is a good thing. It means we have more tools in our programming toolbox to create cool stuff (and make money) with.
We'll be seeing all of these objects throughout this book. These objects are treated with excruciating detail in Appendix A, "ActionScript Reference."
Creating a Class
You don't have to restrict yourself to using only classes that Flash has provided, such as MovieClip. You can create your own classes using constructor functions. This is pretty advanced stuff, and if you can't think of why you'd want to create a new class, don't worry about itusually, only advanced programmers build their own classes. This section is for them. Say you want to create a 1980s band:
function HairBand(p,s) { this.hair = "big"; this.hair_dye = true; this.number_members = p; this.number_synthesizers = s; } function Breakup() { this.hair_dye = false; this.hair = "crew cut"; } // Now, actually create two objects using // the HairBand constructor function. kajagoogoo = new HairBand(3,4); softcell = new HairBand(2,1500); // Create a method for a hairband HairBand.kajagoogoo.partyover = Breakup;