with

Availability

Flash Player 5.

Usage

with (object) {
  statement(s);
}

Parameters

object An instance of an ActionScript object or movie clip.

statement(s) An action or group of actions enclosed in curly braces.

Returns

Nothing.

Description

Statement; lets you specify an object (such as a movie clip) with the object parameter and evaluate expressions and actions inside that object with the statement(s) parameter. This prevents you from having to repeatedly write the object's name or the path to the object.

The object parameter becomes the context in which the properties, variables, and functions in the statement(s) parameter are read. For example, if object is my_array, and two of the properties specified are length and concat, those properties are automatically read as my_array.length and my_array.concat. In another example, if object is state.california, any actions or statements inside the with action are called from inside the california instance.

To find the value of an identifier in the statement(s) parameter, ActionScript starts at the beginning of the scope chain specified by the object and searches for the identifier at each level of the scope chain, in a specific order.

The scope chain used by the with action to resolve identifiers starts with the first item in the following list and continues to the last item:

To set a variable inside a with action, the variable must have been declared outside the with action or you must enter the full path to the Timeline on which you want the variable to live. If you set a variable in a with action without declaring it, the with action will look for the value according to the scope chain. If the variable doesn't already exist, the new value will be set on the Timeline from which the with action was called.

In Flash 5 or later, the with action replaces the deprecated tellTarget action. You are encouraged to use with instead of tellTarget because it is a standard ActionScript extension to the ECMA-262 standard. The principal difference between the with and tellTarget actions is that with takes a reference to a movie clip or other object as its parameter, while tellTarget takes a target path string that identifies a movie clip as its parameter, and cannot be used to target objects.

Example

The following example sets the _x and _y properties of the someOther_mc instance, and then instructs someOther_mc to go to Frame 3 and stop.

with (someOther_mc) {
  _x = 50;
  _y = 100;
  gotoAndStop(3);
}

The following code snippet shows how to write the preceding code without using a with action.

someOther_mc._x = 50;
someOther_mc._y = 100;
someOther_mc.gotoAndStop(3);

You could also write this code using the tellTarget action. However, if someOther_mc were not a movie clip, but an object, you could not use the with action.

tellTarget ("someOther_mc") {
  _x = 50;
  _y = 100;
  gotoAndStop(3);
}

The with action is useful for accessing multiple items in a scope chain list simultaneously. In the following example, the built-in Math object is placed at the front of the scope chain. Setting Math as a default object resolves the identifiers cos, sin, and PI to Math.cos, Math.sin, and Math.PI, respectively. The identifiers a, x, y, and r are not methods or properties of the Math object, but since they exist in the object activation scope of the function polar(), they resolve to the corresponding local variables.

function polar(r) {
  var a, x, y;
  with (Math) {
    a = PI * r * r;
    x = r * cos(PI); 
    y = r * sin(PI/2);
}
trace("area = " +a);
trace("x = " + x);
trace("y = " + y);
}

You can use nested with actions to access information in multiple scopes. In the following example, the instance fresno and the instance salinas are children of the instance california. The statement sets the _alpha values of fresno and salinas without changing the _alpha value of california.

with (california){
  with (fresno){
    _alpha = 20;
  }
  with (salinas){
    _alpha = 40;
  }
}

See also

tellTarget