Wednesday, August 8, 2012

Properties and Methods in ActionScript 3

In the previous lesson, we learned about what classes and objects are. In this lesson, we'll be learning about properties and methods. Properties and methods are things that classes have. For example, here are some of the properties and methods available to the MovieClip class:

MovieClip Class
PropertiesMethods
width
height
play()
stop()

These are just a few of the properties and methods that belong to the MovieClip class, and by extension, these properties and methods are available to all MovieClip objects.

To begin our understanding of properties and methods, it's important to think of objects in programming as being similar to real objects - things around us that we can see and touch, like a teacup, a book, a shoe or a ball. These objects have characteristics - small, large, colorful, etc... For example, a teacup can be small, a dress can be colorful, a pillow can be soft. These objects can also do things - a ball can bounce, a wheel can turn, and a phone can ring. So real objects have characteristics, and real objects can do things.

In programming, even though we can't actually touch these objects, they have characteristics and they can do things as well. These characteristics of programming objects are referred to as properties, while things that objects can do are referred to as methods.

Let's go back to the MovieClip class list of properties and methods posted above. In the left column of the list above, we see that MovieClip objects have the width and height properties. These properties define how wide and how high a specific MovieClip object is. The width and the height of a MovieClip object can be seen as characteristics, because they describe the appearance of a MovieClip. To use an analogy, properties are like adjectives - they describe objects.

In the right column of the list above, we find some of the methods of the MovieClip class - play() and stop(). Methods are basically the tasks that objects can perform. For MovieClip objects, when you talk about the play() method, this refers to the MovieClip playing the animation in its timeline. The stop() method refers to when the MovieClip stops the animation. To use an analogy, if properties are like adjectives, then methods are like verbs - they refer to the actions that objects can do. You'll also notice that methods end in parentheses. Properties do not.

How do we access the properties and methods of an object?
To access the properties and methods of an object, you type in the name of the object, followed by a dot, followed by the property or method that you want to access. This is called dot syntax, because we use dots.

Let's say you have a MovieClip object named square_mc. To access the width property, you would type this:
square_mc.width = 200;
Here, we are accessing the width of the square_mc MovieClip, and we are assigning to it a value of 200. This will make the square_mc object 200 pixels wide.

In some instances, you might just want to retrieve the value of a specific property. In the previous example, we accessed the width property and assigned a new value to it. In this next example, we'll just retrieve the value of the width property, and then compare it to another value. We won't be changing the value whatsoever:
if(square_mc.width >= 200)
{
// do something
}
Here, we have an if statement where Flash is just going to retrieve whatever the current value of the width property of square_mc is, and then compare if it's greater than or equal to 200.

So in some instances, we might want access a property and assign a new value to it. In other instances, we might just want to retrieve a property's current value.

NOTE: Not all properties can be assigned values through code. Properties that cannot be assigned values through code are called read-only properties. You can only check for their current values, NOT assign them with new ones. For example, there is a property of the MovieClip class known as the currentFrame property. The currentFrame property of the MovieClip class tells you the current frame of the MovieClip object that the playhead is on. You won't be able to assign a value to this property. The value that it's going to have, is always just dependent on the movement of the playhead within the MovieClip object's timeline.

In this next example, we're accessing the stop() method. Here, we are telling the square_mc MovieClip to stop playing:
square_mc.stop();
Provided that the square_mc MovieClip has some animation in its timeline, then this line will cause that animation to stop.

In some cases, some methods need some extra information. You can pass this extra information by placing it inside the parentheses of the method. For example, the MovieClip class has a method called gotoAndStop(). This method tells the MovieClip to move to a specific frame within its timeline and then stop right there. So when you try to use the gotoAndStop() method, you don't just call the method, you have to give it some extra information. For the gotoAndStop() method, you also have to say which frame number to go to, because you could go to frame 5 or frame 12 or whatever other frame you want. How is Flash going to know unless you specify which frame to go to? So you need to pass that information by putting it inside the parentheses of the method. So let's say we wanted the square_mc MovieClip to go to frame no. 2 of its timeline and stop there, then we would type:
square_mc.gotoAndStop(2);
This will instruct the MovieClip to go to the 2nd frame in its timeline and stop.

And those are just a few examples of properties and methods in ActionScript 3. To recap:
  • properties are like adjectives - they describe the object
  • methods are like verbs - they are the actions that an object can do
  • methods end in parentheses, properties do not
  • you can access the properties and methods of an object using dot syntax
  • some properties are read-only - you cannot assign values to them using code
  • some methods require added information, which you pass to the method by typing the needed information inside the method's parentheses

1 comment:

  1. I really like the way you simplify complex things :)
    another great explanation.

    ReplyDelete