Changing Color

So far we have learned to move and change the size and rotation of our shapes. Now we will learn to change their colors. A) Begin as always by opening our html page and begining our php code block
<html>
<body>
<?php

B) Next, create our shape as usual

$myShape1=new SWFShape();
$myShape1->setLine(5,0,0,255);
$myShape1->setRightFill(255,0,0);
$myShape1->movePen(-30,-30);
$myShape1->drawLine(60,0);
$myShape1->drawLine(0,60);
$myShape1->drawLine(-60,0);
$myShape1->drawLine(0,-60);

C) Then, we create our movie as always

$myMovie=new SWFMovie();
$myMovie->setDimension(200,200);
$myMovie->setBackground(255,255,255);

D) Now add our shape to our movie and move it into place

$movingSquare=$myMovie->add($myShape1);
$movingSquare->moveTo(100,100);

E) Next, we start a loop to go through the steps of our color change, don't forget the opening bracket at the end!

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

F) Our loop is creating a form of animation for our color change, so we need it to advance the frame as with previous animations

$myMovie->nextFrame();

G) Next, we use the multColor function to multiply our shapes color values. By using the loop's value, the $i variable, we can make the change gradual. Here I have set it to multiply the red color by an increasingly smaller value, making the red slowly fade away. Notice that we multiply the green and blue values by 1 so that we do not change them

$movingSquare->multColor(1.0-$i/20, 1.0, 1.0);

H) Don't forget to close off the loop with the closing bracket!

}

I) Let's do it again for the blue color in our shape, just for practice

for($i=0; $i<20; $i++){
$myMovie->nextFrame();
$movingSquare->multColor(1.0, 1.0, 1.0-$i/20);
}

J) Then we finish up by saving our movie and displaying it with our object tags as always

$myMovie->save("lesson9.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="lesson9.swf">
<EMBED src="lesson9.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 do simple morphs from one shape to another!

Code summary for lesson 9:

Result: