How to draw a line using ActionScript

by Elis Frugalo.

Share
|
Homepage | Submit your article | Contact | TOS
More articles on flash  

You are here: Categories » Computers and technology » Flash

Use the lineStyle( ) method to specify the thickness, color, and transparency of the line. Then use the lineTo( ) method to draw a line from the current pen position to a destination point.

Before you can draw anything using ActionScript, you need to tell Flash what kind of line style to use. The line style consists of the line's thickness, color, and alpha value (opacity), and you can set these values using the lineStyle( ) method on the movie clip in which you wish to draw. The line's thickness should be specified in pixels, but any value less than 1 draws lines with hairline thickness. Color values should be specified as numbers. The alpha value should be anywhere from 0 (completely transparent) to 100 (completely opaque).

// Set a 1-pixel, blue, completely opaque line style.
   myMovieClip_mc.lineStyle(1, 0x0000FF, 100);

The most basic type of drawing that you can do with ActionScript is drawing a straight line. Flash uses the current pen position as the starting point, so you need to provide only the coordinates of the destination point. Use the MovieClip.lineTo( ) method to create a line from the current pen position to the specified destination point:

// Draws a line from the current pen position to (100,100) within the coordinate
   // system of myMovieClip_mc.
   myMovieClip_mc.lineTo(100, 100);

When you use ActionScript methods to draw, all the lines and fills are drawn within the movie clip from which the methods are invoked. For example, in the preceding code, the line is drawn within myMovieClip_mc. Additionally, the line style must be defined for each movie clip. If you define a line style for myMovieClip_mc, but then you try to use the lineTo( ) method with another movie clip instance, Flash does not draw anything.

As mentioned previously, when you use the Drawing API methods such as lineTo( ), Flash draws the line beginning at the current pen position. If you have not otherwise moved the pen (by calling a lineTo( ), curveTo( ), or moveTo( ) method), the pen is positioned at the origin of the movie clip's coordinate system, point (0,0). The origin is the clip's registration point, which resides at the clip's center by default. You can move the pen without drawing a line by using the moveTo( ) method. The moveTo( ) method simply relocates the pen to the coordinate you specify.

// Move the pen in myMovieClip_mc to (300,30).
   myMovieClip_mc.moveTo(300, 30);

The moveTo( ) method is important in situations in which you want to begin drawing from a point other than the movie clip's center or draw lines or shapes without necessarily connecting all the lines:

// Create an empty movie clip to act as the drawing canvas.
   _root.createEmptyMovieClip("myMovieClip_mc", 1);

// Set a 1-pixel, black, completely opaque line style.
myMovieClip_mc.lineStyle(1, 0x000000, 100);

// Draw a dashed line using a series of lines 
   and spaces.
   myMovieClip_mc.moveTo( 0, 0);
   myMovieClip_mc.lineTo(10, 0);
   myMovieClip_mc.moveTo(15, 0);
   myMovieClip_mc.lineTo(25, 0);
   myMovieClip_mc.moveTo(30, 0);
   myMovieClip_mc.lineTo(40, 0);
   myMovieClip_mc.moveTo(45, 0);
   myMovieClip_mc.lineTo(55, 0);
Leave a comment or ask a question
Total comments: 0

Flash Disclaimer

  • The e-articles directory is not responsible for any and all copyright infringements by writers and authors. If you suspect the information contained by this page for any copyright infringements, please contact us to investigate the issue
How to build an animated button using Flash - Time: 15 Minutes. Difficulty Level: Intermediate Requirements: Flash 8 Assumed Knowledge: Basic action (more...)
Enhancing Standalone Projector - You want to create an enhanced Standalone Projector with features such as borderless playback, custom titles, no Flash menus, and so on. Use a third-party tool such as SWF Studio or SWFKit to c (more...)
Creating Flash Buttons - Buttons are one of the three main types of symbols that you use in Flash. The others are movie clips and graphics. Making a New Button There are many ways to create a button. One (more...)
Bouncing Ball Script - Start a new Flash movie. Create a movie clip that has a ball graphic inside it. You can name the instance of the movie clip, myClip, but our code will not depend on the name of the clip. (more...)
ActionScript: Creating Reusable Code - You want to perform a series of actions at various times without duplicating code unnecessarily throughout your movie. Create a function and then call (i.e., invoke) it by name whenever you (more...)
3D Scaling with ActionScript - Although Flash is not capable of real 3D graphics, the kind seen in popular computer games, you can create the illusion of 3D by using scaling. Scaling an object is a great way to give your (more...)
The Dot Syntax - Something else that you will be seeing a lot of as you learn ActionScript is dot syntax. Dot syntax is a way of grouping objects and functions that is used in many object-oriented programming (more...)
Movie Clip Scripts - Movie clip scripts, like button scripts, use handlers. Instead of using the on keyword that button scripts use, we will use the onClipEvent keyword. Here is an example of a movie clip script: (more...)
Decoding an RGB Value - You want to extract the red, green, and blue components from an RGB value returned by Color.getRGB( ). Use the bitshift right and bitwise AND operators. You can extract the red, gre (more...)
ActionScript: Generalizing a Function to Enhance Reusability - You want to perform slight variations of an action without having to duplicate multiple lines of code to accommodate the minor differences. Add parameters to your function to make it flexib (more...)

 
free content
    Copyright © 2006 - 2012 e-articles.info.
The texts, articles and tutorials in the directory are property of their respective owners and authors.