ActionScript coding conventions 759
In this code, you attach a movie clip instance from the library and use the with statement to
modify its properties. When you do not specify a variable’s scope, you do not always know
where you are setting properties, so your code can be confusing. In the previous code, you
might expect
someVariable to be set within the circle1Clip movie clip, but it is actually
set in a timeline of the SWF file.
It is easier to follow what is happening in your code if you explicitly specify the variables
scope, instead of relying on the
with statement. The following example shows a slightly
longer, but better, ActionScript example that specifies the variables scope:
this.attachMovie("circleClip", "circle1Clip", 1);
circle1Clip._x = 20;
circle1Clip._y = Math.round(Math.random()*20);
circle1Clip._alpha = 15;
circle1Clip.createTextField("labelTxt", 100, 0, 20, 100, 22);
circle1Clip.labelTxt.text = "Circle 1";
circle1Clip.someVariable = true;
An exception to this rule is, when you are working with the drawing API to draw shapes, you
might have several similar calls to the same methods (such as
lineTo or curveTo) because of
the drawing API’s functionality. For example, when you draw a simple rectangle, you need
four separate calls to the
lineTo method, as the following code shows:
this.createEmptyMovieClip("rectangleClip", 1);
with (rectangleClip) {
lineStyle(2, 0x000000, 100);
beginFill(0xFF0000, 100);
moveTo(0, 0);
lineTo(300, 0);
lineTo(300, 200);
lineTo(0, 200);
lineTo(0, 0);
endFill();
}
If you wrote each lineTo or curveTo method with a fully qualified instance name, the code
would quickly become cluttered and difficult to read and debug.