14Object is the most general word in actionscript. it is a core datatype, and the base from which all other complex types are constructed. objects are relatively simple, but very flexible. they collect a number of values, but instead of storing them in sequential order like an array, an object stores its values under property names. we've already made use of object properties many times over:
Math.PIStage.widthlength property_alpha propertyx propertywhat we haven't done is create them from scratch yet.
var obj:Object = { myProperty : myValue };or via a constructor function:
var obj:Object = new Object(); obj.myProperty = myValue;property values can any other value type, whether native:
obj.propertyName :
trace(Math.PI); var clark:Object = { weakness : "kryptonite" }; trace(clark.weakness);or alternatively, using an associative array syntax—
obj["propertyName"] :
trace(Math["PI"]); var clark:Object = { weakness : "kryptonite" }; trace(clark["weakness"]);when you don't know all the property names of an object, or need to look at them all, you can use a
for .. in loop:
var obj:Object = { a : 1, b : "two", c : [3,2,1], d: false }; for (propertyName:String in obj) { var val = obj[propertyName]; trace(propertyName +" : " +val); }the loop looks at each exposed property in the object, and assigns the property name to the iterator variable you declare. then you can access the value assigned to the property using the associative array syntax. for custom-built objects, all properties will be exposed, but for built-in objects like MovieClip, not all properties are exposed to a
for .. in loop.
_parent property.
hopefully you've seen by now that using objects can be very convenient. we don't need to worry much about how things work, just what controls are provided. complexity is encapsulated within the object, and exposed through a simpler API of properties and methods with explicit datatypes and method signatures.
just as variables become handy as soon as we start using the same value in multiple places, and functions become advantageous as soon as we have a few statements that we'd like to be able to reuse, objects are useful for grouping a set of conceptually related properties and methods, and controlling how they can be accessed and / or modified.
breaking a programming task into a structure of cooperating objects is called object-oriented programming.
it is a methodology of organization and little more. but organization is a highly effective tool for addressing
complexity. some programming tasks are trivial enough that no extra organization is needed. a simple timeline
narrative may only need a few gotoAndPlay() function calls. but bigger projects generally benefit
from an object-oriented technique in the following ways:
by being able to define our own objects, we can implement new datatypes to complement and expand on the native types offered by actionscript, while still being able to take advantage of compile-time type checking. we can build complex applications, but if we engineer them from simple, well-defined parts, those parts can be recombined in other ways to build other applications with less effort.