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

B) Next, create our shape as usual

$myShape1=new SWFShape();

C) Then, we create our movie as always

$myMovie=new SWFMovie();

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


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


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++){
$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


<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase=",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="">

Next lesson we will learn to do simple morphs from one shape to another!

Code summary for lesson 9: