Today's free ActionScript tutorial will be a little different from the rest. Firstly, it is the response to a suggestion we had from one of visitors, which read:
One thing that I can find little on anywhere is spreading your code and classes across .as files. For example addChild in another .as starts a new container in that .as and doesn't append to the main .as file. I understand the idea of classes and the syntax, but am unable to find coded examples with multiple .as files.
We will be looking at the difference in addChild later, but first I feel it is my duty to give some context about classes, what they are and what they do. Because this is something generally taught in computer science degree courses, I can't be bothered to write it all out again. Instead, I am going to be drawing excerpts from a well-known online encyclopaedia, which are released under the GNU Free Documentation Licence.
What are classes?
In object-oriented programming, a class is a programming language construct that is used as a blueprint to create objects. This blueprint includes attributes (variables) and methods (functions) that the created objects all share.
Usually, a class represents a person, place, or thing - it is an abstraction of a concept within a computer program. Fundamentally, it encapsulates (combines into one object) the state and behavior of that which it conceptually represents.
The general idea of encapsulation is a simple one: when you drive a car, there are certain bits of the car accessible to you, and there are bits you never see or touch. They perform important functions but you don't control them. You turnLeft(), which causes the steering wheel to twistSteeringColumn() which turnsAxle(), and the cause steers to the left. You didn't have access to the steering column, but you controlled it using the human-understandable interface that is the steering wheel.
How do they interact?
The .fla communicates with the document class and actions panel; they call other classes. The other classes can access the stage and its attributes, however. Inheritence is not shown
In object-oriented programming, inheritance is a way to form new classes (instances of which are called objects) using classes that have already been defined. The new classes, known as derived classes, take over (or inherit) the attributes and behavior of the pre-existing class.
In ActionScript, inheritance is triggered by the extends keyword. class X extends Y makes X inherit all the properties and methods of Y.
Well, that was long and boring, but now we get to the farm part - building a toy farm. Well, techincally speaking I've already built it for you - I would post the code up here so everyone could see it, but there are simply to many files. Grab your own farm and take a look. (Yes, playing with a toy farmyard is really quite addictive. Sure boss, I'll get back to work straight away...) Cow and Sheep extend Animal - and inherit its grow() method and its itsHeight attribute.
As for addChild, we can see what happens like this:
Our example actually features two extra classes: the image is addChild'ed to the Cow or Sheep; the Cow/Sheep is then addChild'ed to the Farm; the Farm is addChild'ed to the document class (FarmManager) and the document class is automatically addChild'ed to the display list, which is why the images become visible. Be wary though - x and y values are relative to the layer above, not to the stage! (If you try it, you'll see what I mean.)
Well, I hope that's answered your question!