MovieClip.lineTo( ) Method

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 18.  ActionScript Language Reference
MovieClip.lineTo( ) Method Flash 6

draws a straight line at runtime
mc.lineTo(x, y)

Arguments

x

The horizontal position of the endpoint of the line to be drawn, as a floating-point number. Measured relative to mc's registration point.

y

The vertical position of the endpoint of the line to be drawn, as a floating-point number. Measured relative to mc's registration point.

Description

The lineTo( ) method draws a straight line from the current drawing pen position to the point (x,y) (for a description of the drawing pen see MovieClip.moveTo( )). The most recent call to lineStyle( ) determines the stroke characteristics of the line (thickness, color, and alpha). If the lineStyle( ) is undefined, lineTo( ) moves the drawing pen position but does not draw a line on screen (exactly as if moveTo( ) had been called). The line drawn appears above all Drawing API content already in mc, but beneath all other content in mc (e.g., movie clips, shapes, text, and buttons placed at authoring time, or content attached at runtime via attachMovie( ), duplicateMovieClip( ), or createTextField( )). If either of lineTo( )'s arguments is missing, the operation fails silently.

The following code creates a movie clip named drawing_mc and draws a 2-point black line downward to the point (0,100) from the drawing pen's current position (0,0). Until the drawing pen's position is modified by moveTo( ), lineTo( ), or curveTo( ), the drawing pen's position is the registration point of mc, or the upper-left corner of the Stage if mc is _root.

this.createEmptyMovieClip("drawing_mc", 1);  // Create a clip to draw in drawing_mc.lineStyle(2);                     // Set the stroke to 2 point, black drawing_mc.lineTo(0, 100);                   // Draw the line

After a line or curve is drawn, the drawing pen remains at the end point of the line or curve. Hence, a continuous jagged line can easily be drawn using a series of lineTo( ) and/or curveTo( ) calls:

// Create a clip to draw in this.createEmptyMovieClip("drawing_mc", 1);     // Set the stroke to 1 point, red drawing_mc.lineStyle(1, 0xFF0000);     // Draw a jagged line as a series of line segments drawing_mc.lineTo(50, 100); drawing_mc.lineTo(-20, 150); drawing_mc.lineTo(75, 200); drawing_mc.lineTo(50, 300);

Multiple calls to lineTo( ) and/or curveTo( ) can be used similarly to draw closed shapes. For details, see MovieClip.beginFill( ).

Of course, movie clips that contain Drawing API content can be manipulated as usual. For example, this code makes our drawing_mc clip spin:

drawing_mc.onEnterFrame = function () {   this._rotation++; }

Reader Exercise: Notice that lineTo( ) accepts absolute coordinates, not relative coordinates. Write a custom method, lineToDelta( ), that accepts relative coordinates. For example, mc.lineToDelta(5, 10) would draw a line to a point 5 pixels to the right of, and 10 pixels below, the current pen position. (Hint: See Example 18-3 under MovieClip.moveTo( ), which stores the pen's current position.)

See Also

MovieClip.beginFill( ), MovieClip.beginGradientFill( ), MovieClip.clear( ), MovieClip.curveTo( ), MovieClip.endFill( ), MovieClip.lineStyle( ), MovieClip.moveTo( )



    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