Loop Lesson

In this lesson, Jerry discusses the use of loops in lessons 8a, b, and c, as shown in the Notes to those lessons.

Many of the methods used to animate various objects are simple functions repeated over and over. In these basic frame by frame animation examples, we have used the same statements over and over in order to produce an entire movie. This works, but it would not be practical for a 100 or 1000 frame movie you may eventually create.

So, we go back to the php manual and study loops a bit more. Then, we can take the repeating statements and boil them down to one single loop statement, producing the same results as before, with a smaller file size.

A) Use the same code as the other lesson 8 examples, up to and including the point where we add our shape to the movie

<html>
<
<?php
$myShape1=new SWFShape();
$myShape1->setLine(5,0,0,255);
$myShape1->setRightFill(255,255,0);
$myShape1->movePen(-30,-30);
$myShape1->drawLine(60,0);
$myShape1->drawLine(0,60);
$myShape1->drawLine(-60,0);
$myShape1->drawLine(0,-60);
$myMovie=new SWFMovie();
$myMovie->setDimension(460,80);
$myMovie->setBackground(255,0,0);
$movingSquare=$myMovie->add($myShape1);
$movingSquare->scale(0.5,0.5);
$movingSquare->moveTo(40,40);


B) Now, we define our loop, beginning with a starting number, then a condition setting how long to loop, then the amount to increase our loop each time. Remember the opening bracket to start your loop code section.

for($i=0; $i<12; $i++){


C) Next, we include the code to be repeated until the loop condition is met, 12 times in this example. Notice that I included all the motions covered in the combined lesson 8s to demonstrate how looping makes it easier to create complex motions.

$myMovie->nextFrame();
$movingSquare->scale(1.1,1.1);
$movingSquare->rotate(-15);
$movingSquare->move(40,0);


D) Don't forget to close the loop code section with a closing bracket.

}

E) Then we save and output our movie with the object tags as usual

$myMovie->save("lesson8d.swf");
?>
<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0" ID=objects WIDTH=460 HEIGHT=80>
<PARAM NAME=movie VALUE="lesson8d.swf">
<EMBED src="lesson8d.swf" WIDTH=460 HEIGHT=80 TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">
</EMBED>
</OBJECT>
</BODY>
</html>


Next lesson, we will learn to change the color and transparency of our movies contents!

Code summary for lesson 8d: