The FOR Loop - 06ForLoop.flaThe For Loop is used when you need to repeat an operation several times. Which kind of loop ???
This program creates a movie clip and draws a graph with grid lines.
|
|||||||
|
|||||||
Set up your Flash MX environment like this ... |
|||||||
Enter this code
on (release)
{
_root.createEmptyMovieClip("myGraph", 1);
with (_root.myGraph) {
lineStyle(1, 0x000000, 100); // 1 thickness, 0x000000 colour, 100% alpha
// HORIZONTAL GRID LINES
// START-- STOP---- STEP--------
for (yy = 50; yy < 400; yy = yy + 20)
{
moveTo( 0, yy); // Start position
lineTo(500, yy); // Straight line to this position
}
moveTo(0, 50 + (-250 / 13) * (-250 / 13)); // Start position
// PLOT THE GRAPH
// START---- STOP---- STEP-------
for (xx = -250; xx < 250; xx = xx + 1)
{
yy = (xx / 13) * (xx / 13); // yy = xx squared
lineTo(250 + xx, 50 + yy);
}
}
|
|||||||
Here is the example movie
You can download the source code. |
|||||||
Tasks
|
|||||||
NotesExample
The For Loop has three parts. These are start, stop and step. The example above counts from 10 to 19. On the first time round the loop, ii = 10. Next time round ii = 11. Each time round the loop, ii increases its value by one.
|
© C N Bauers (2004) - You may use these materials for your self education. Educational institutions may use these materials too.