Tutorials
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 |
Properties | Methods |
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
-
Classes And Objects In Actionscript 3
In this lesson, I'll be explaining what classes and objects are. We'll be understanding these terms within the context of ActionScript, but classes and objects really are concepts that are part of most programming languages. So the general idea...
-
The Flash As3 Tween Class
In this tutorial, we're going to learn how to use the Flash AS3 Tween Class. The tween class lets you create tween animation using code. Step 1 Let's first create a movie clip that we will use with our ActionScript 3 tween. Draw a small circle...
-
How To Change The Size Of A Movie Clip Using Actionscript 3 - Assigning As3 Instance Names And Modifying Some As3 Movie Clip Properties
by Alberto Medalla Lecturer, Ateneo de Manila University In this lesson, I'm going to discuss the concept of instance names and how they are needed in ActionScript in order to control display objects (objects that can be seen on the stage). Let's...
-
Simple As3 Collision Detection Using The As3 Hittestobject() Method
In this tutorial, we're going to learn how to create basic AS3 collision detection using the AS3 hitTestObject() method of the MovieClip class. Collision detection refers to the process of checking whether 2 or more objects are hitting each other...
-
As3 Dragging And Dropping Tutorial - As3 Startdrag() And As3 Stopdrag()
Exercise File: as3-drag-and-drop.fla - Flash CS4/CS5 as3-drag-and-drop.fla - Flash CS3 In this tutorial, we're going to learn how to create simple AS3 drag and drop functionality using the AS3 startDrag() and AS3 stopDrag() methods. The exercise file...
Tutorials