Lesson 8d - Adding
motion (Loops)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>
<body>
<?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, begining 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("lesson8-looped.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="lesson8-looped.swf">
<EMBED src="lesson8-looped.swf" WIDTH=460 HEIGHT=80 TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">
</OBJECT>
</body>
</html>
Next lesson, we will learn to change the color and transparency of our movies contents!
<html>
<body>
<?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);
for($i=0; $i<12; $i++){
$myMovie->nextFrame();
$movingSquare->scale(1.1,1.1);
$movingSquare->rotate(-15);
$movingSquare->move(40,0);
}
$myMovie->save("lesson8-looped.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="lesson8-looped.swf">
<EMBED src="lesson8-looped.swf" WIDTH=460
HEIGHT=80 TYPE="application/x-shockwave-flash"
PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">
</OBJECT>
</body>
</html>