O'Reilly logo

Learning ActionScript 3.0 by Zevan Rosser, Rich Shupe

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Geometry Package

The flash.geom package is a handy set of utility classes that help create and manipulate points, rectangles, and data used to transform the appearance of objects. Here we'll focus on three of its classes: Points, Rectanges, and Matrices. We'll revisit the Geometry package later when discussing color in the next chapter.

Creating Points

The Point class allows you to automatically create point data without having to define custom objects each time or use arrays. Faking a point using linear arrays requires that you populate and retrieve the array data in the correct order. Here is an example:

var arrayPoint:Array = new Array(0, 0);
trace(arrayPoint[0], arrayPoint[1]);

Objects, which sometimes take the form of associative arrays, are a bit clearer because you can associate x and y property names with the values. Here are two examples:

var objPoint:Object = {x:0, y:0};
trace(objPoint.x, objPoint.y);

var objPoint2:Object = new Object();
objPoint2.x = 0;
objPoint2.y = 0;
trace(objPoint2.x, objPoint2.y);

All these examples correctly trace the values of 0 for x and y to the Output panel; however, in these examples, you still can't benefit from strong data typing and error reporting. For example, it is possible to store many data types in arrays.

An instance of the Point class comes complete with x and y properties, and creating an instance is as easy as using the new operator. Using an empty constructor, as seen in the first line of the following code block, will automatically ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required