organization
programming is all about organization. let's review all of the data structures we've covered so far
and see how they enable us to organize, by packaging and labeling.
variables
variables let us label values, and then use the label in place of the value.
-
by minimizing the appearance of magic numbers in our code, changes requires updates in fewer places.
-
by allowing us to refer to values by a meaningful label, variables make code easier to read and debug.
arrays
arrays allow us to gather multiple values into a single variable that stores the values in order.
-
arrays go hand in hand with loops for addressing each value in an array
-
arrays can collect values of any type or combination—other arrays, functions, variables, objects, etc.
functions
functions allow us to gather multiple statements into a single variable and
then execute the statements from multiple places without rewriting them.
-
functions can call other functions, even themselves, to break complex procedures into manageable
execution steps.
-
functions can be tied to events by assigning them as the value of object event properties.
objects
objects allow us to collect multiple values into a single variable that stores the values by name.
-
objects can collect values of any type or combination—simple values, arrays, functions, other objects, etc.
-
objects provide better storage than arrays for values that aren't naturally sequential, since the values
can be accessed by property name, which makes code clearer.
-
objects are very useful for registering as listeners to event broadcasters like Key and Stage, etc.
since they can contain properties that match the event names, and have a function as the property's value.
-
some macromedia functions require object parameters
movie clips
movie clips allow us to collect graphics and code into an environment that has a concept of time.
-
code can create and control graphical properties of the movie clip, and jump to various points on the timeline.
-
movie clips can be nested inside each other and referenced via instance name and the
_parent property.
object oriented programming
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 interface 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:
- reusability
- more natural planning
- simpler maintenance
- better testing
- efficient delegation of tasks to a team of developers
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.