
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Classes and Object-Oriented Programming
|
287
Ball.prototype.moveClipTo = function (x, y) {
// Notice the use of this to refer to the ball being moved.
this.xPosition = x;
this.yPosition = y;
};
We’ve now created the all properties and methods of our Ball class, so our work is
done! The final code is shown in Example 12-2. Attach the example code to frame 1
of a new movie; then choose Control
➝ Test Movie to see it in action.
Invoking a method and defining a method have similar syntax but very different
results. It’s crucial to understand the difference. When defining a method, omit the
parentheses:
SomeClass.prototype.someMethod = someFunction;
When invoking a method, include the parentheses:
someObj.someMethod( );
The difference in syntax is subtle. Make sure to get it right.
Example 12-2. The completed Ball class
// The Ball( ) class constructor.
function Ball (radius, color, xPosition, yPosition) {
// Create Ball properties.
this.radius = radius;
this.color = color;
this.xPosition = xPosition;
this.yPosition = yPosition;
}
// The Ball.getArea( ) method.
Ball.prototype.getArea = function () {
// The keyword this means the ball on which getArea( ) was invoked.
return Math.PI * this.radius * this.radius;
};
// The Ball.moveClipTo( ) method.
Ball.prototype.moveClipTo = function (x, y) {
// Notice ...