Drawing Shapes with Fills

[ LiB ]

Drawing Shapes with Fills

Drawing shapes with fills is not that much harder than drawing empty shapes in ActionScript. It's just a matter of learning how to use two more commands. These are the beginFill and endFill methods of a Movie Clip.

I have designed another project for this section, GDA_PROG7.6.fla, which draws a filled triangle. Check it out in Figure 7.7, which shows you how the program internally fills the shape.

Figure 7.7. Drawing a filled triangle

graphic/07fig07.gif


Now that you know what you're going to learn in this section, let's jump right into the code. Take a look at the following listing.

 // Game Development with ActionScript // By Lewis Moronta (c) 2003 // Chapter 7, Drawing with a Script // This demo demonstrates how to create // filled shapes with the beginFill // and endFill methods. // This line specifies the // color that the fill should // be and it also tells Flash // to draw all closed shapes // with a fill. beginFill (0xFFFF00, 80);  // This sets the line width to 2 // Color to Blue and 100 percent opaque lineStyle (2, 0x0000FF, 100); // This draws a triangle moveTo (160, 10); lineTo (320, 190); lineTo (0, 190); lineTo (160, 10); // This command ends the filling // and prevents subsequent commands // from being filled. endFill(); 

The beginFill line you see in this listing doesn't do much but tell Flash to set up the fills:

 // This line specifies the // color that the fill should // be and it also tells Flash // to draw all closed shapes // with a fill. beginFill (0xFFFF00, 80); 

The first parameter indicates the fill's color and the second parameter is the fill's opacity in percent. As I am applying fill style to the _root Movie Clip (which is the highest in the hierarchy), this style will apply for any Movie Clips created within it.

And now, in this following line, the line style is applied:

 // This sets the line width to 2 // Color to Blue and 100 percent opaque lineStyle (2, 0x0000FF, 100); 

As you already noticed, the line thickness is 2 points, the color is blue, and it's completely opaque.

And finally, the moveTo and lineTo commands below draw out the shape (of the triangle). When the script is run, the fill will be there.

 // This draws a triangle moveTo (160, 10); lineTo (320, 190); lineTo (0, 190); lineTo (160, 10); 

Before looking at the last line, notice how drawing a line to the exact point where it was drawn from closes the shapethis part could be omitted because the fill methods automatically close the shape or shapes that you're trying to fill by creating a line between the last point you drew and the first point you drew.

The last command generally stops all fills:

 // This command ends the filling // and prevents subsequent commands // from being filled. endFill(); 

Any lineTo commands after this point won't generate any fills because of the endFill command. Sweet and simplejust the way I like it.

[ LiB ]


Game Development with ActionScript
Game Development with ActionScript
ISBN: 1592001106
EAN: 2147483647
Year: 2004
Pages: 162
Authors: Lewis Moronta

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