Understanding Objects, Classes, and Scope


This section introduces you to some new concepts. It will be useful for you to be exposed to the ideas here before moving on to the following lessons. We will not go into much detail with objects and classes here because there are later lessons devoted to these topics.

Objects and Classes

An object is a container. When created, the object is empty. Variables can be added to this object. A variable that "lives" in an object is called a property, but you'll still frequently see it referred to as a variable.

A function is a grouping of code that is given a name. By using special syntax, that grouping of code can be executed any number of times and at any time. A function can be added to an object. A function that lives in an object is called a method.

Now imagine an object that has certain properties and methods that are useful for a special purpose. For instance, imagine an object that had these properties: firstname, lastname, and age. This object also has the methods speak(), eat(), and sleep(). This fake object represents a person. The person can have a first name, a last name, and an age. This person can be told to speak, to eat, and to sleep.

What if we want to have an object like this, with these specific properties and methods, but for an unlimited number of unrelated people? It would be a pain to start off with some basic object and create properties and methods every time we need them. That's where a class comes in, which is a blueprint for a custom object. The previous example might be called the Person class. If we defined a new class called Person with the properties and methods described here, we can easily create new instances of this class when we need them.

In Flash, there are many classes built in. Here are a few that you've already seen: MovieClip, String, Number, Boolean, and Button. Some of the classes have an asset on the screen (MovieClip and Button), but most do not. Some properties of the MovieClip class are as follows:

  • _x The x position of the movie clip

  • _y The y position of the movie clip

  • _rotation The rotation in degrees of the movie clip

  • _name The instance name of the movie clip

As a quick recap, we have seen that

  • An object is an empty container to which you can add properties and methods.

  • A class is a blueprint for a new copy of an object that has a custom set of properties and methods.

In Lesson 5, "Built-in Classes," and Lesson 6, "Custom Classes," you will learn a lot more about the built-in classes and will create your own custom classes!

Scope

Let's say you have two movie clips on the stage with the instance names cat_mc and dog_mc. As mentioned, all instances of the MovieClip class have a property called _x. Because a property is just a variable on an object, here we have two movie clips, and so we have two _x properties.

The _x property represents the x position (horizontal) of the movie clip. It makes sense that the value of the _x property of dog_mc should not be the same as that of cat_mc, unless of course they are on top of each other. In this case, we have two variables of the same name that exist separately.

The scope of a variable is where that variable exists. No other variable of the same name can exist within the same scope. A variable is scoped to the object in which it is created. An _x property was created separately for the cat_mc object and the dog_mc object. Those properties are scoped to the object in which they reside.

Even if something is scoped to a specific object, it can still be seen by other scopes if it is referenced using dot syntax. Consider that this ActionScript on Frame 1 of the timeline contains cat_mc and dog_mc:

   var cat_x:Number = cat_mc._x;    var dog_x:Number = dog_mc._x;


These lines of code create two variables whose values are the _x properties of cat_mc and dog_mc. It illustrates how you can reference the properties in one scope from another.




Macromedia Flash 8 ActionScript Training from the Source
Macromedia Flash 8 ActionScript: Training from the Source
ISBN: 0321336194
EAN: 2147483647
Year: 2007
Pages: 221

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net