MovieClip._x Property

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 18.  ActionScript Language Reference
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:

  • If mc resides on the main timeline, _x is measured relative to the Stage's left edge. For example, an _x of 20 indicates that the registration point of mc is 20 pixels to the right of the Stage's left edge; -20 indicates 20 pixels to the left.

  • If mc resides on another movie clip instance's timeline, _x is measured relative to the registration point of that parent instance. For example, an _x of 20 indicates that the registration point of mc is 20 pixels to the right of its parent instance's registration point; -20 indicates 20 pixels to the left.

  • If mc is the main movie, _x is the horizontal offset of the entire .swf document, relative to the Stage's left edge. For example, an _x of 200 indicates that the contents of the Stage are offset 200 pixels to the right of their author-time position; -200 indicates 200 pixels to the left.

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



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net