Table of Contents

MovieClip._x Property Flash 4

horizontal location of a clip or movie, in pixels read/write
mc._x

Description

The floating-point _x property always indicates the horizontal position of mc's registration point, but it is measured relative to one of three possible coordinate spaces:

The _x property (along with all horizontal coordinates in Flash) increases to the right and decreases to the left. Fractional _x values are approximated in Flash with antialiasing (blurring). Flash's basic unit of measure is a twentieth of a pixel (a "twip"), so the shortest distance a clip can be moved horizontally is .05 pixels. Smaller increments are ignored:

trace(ball_mc._x);    // Displays: 0
ball_mc._x += .01;
trace(ball_mc._x);    // Still displays 0. The assignment had no effect.

If mc is contained by an instance that is scaled and/or rotated, the coordinate space it inhabits is also scaled and/or rotated. For example, if mc's parent is scaled by 200% and rotated 90 degrees clockwise, _x will increase in the downward direction rather than to the right, and a single unit of _x will actually be 2 pixels instead of 1.

Because switching between instance and main movie coordinate spaces can be cumbersome, the MovieClip object provides the localToGlobal( ) and globalToLocal( ) methods for performing coordinate-space transformations.

Example

The following onEnterFrame( ) handler causes ball_mc to move 5 pixels to the right with each passing frame (assuming that its coordinate space hasn't been altered by transformations to its parent):

ball_mc.onEnterFrame = function () {
  this._x += 5;
}

Positioning clips via _x and _y is a fundamental task in visual ActionScript programming. The following example adds to all movie clips a follow( ) method that moves a clip towards a point at a fixed rate specified by the custom speed property (many other motion samples can be obtained from the online Code Depot):

// Method: MovieClip.follow()
// Moves a clip toward the point (leaderX, leaderY)
MovieClip.prototype.follow = function (leaderX, leaderY) {
  // Move only if we're not at the destination
  if (this._x != leaderX || this._y != leaderY) {
    // Determine the distance between the clip and leader
    var deltaX = this._x - leaderX;
    var deltaY = this._y - leaderY;
    var dist = Math.sqrt((deltaX * deltaX) + (deltaY * deltaY));
   
    // Allocate speed between X and Y axes
    var moveX = this.speed * (deltaX / dist);
    var moveY = this.speed * (deltaY / dist);
   
    // If the clip has enough speed to overshoot the destination, just go 
    // to the destination. Otherwise move according to the clip's speed.
    if (this.speed >= dist) {
      this._x = leaderX;
      this._y = leaderY;
    } else {
      this._x -= moveX;
      this._y -= moveY;
    }
  }
}
   
// Usage...
butterfly_mc.speed = 10;
butterfly_mc.onEnterFrame = function () {
  this.follow(_root._xmouse, _root._ymouse);
}

See Also

MovieClip.globalToLocal( ), MovieClip.localToGlobal( ), MovieClip._xscale, MovieClip._y


Table of Contents