Action Script

By now, you know enough to create shapes, text, and sounds for your swf movies, and how to add frames and animate these objects. With these basic skills, you can create some truly amazing movies. Now let's take the next step and learn some advanced concepts.

Sprites are mini-movies contained within your main swf movie, and can do virtually anything a full movie can do. Sprites have their own time-line and run independent of the main movie's timeline or other sprites.

Once we have defined a sprite and added it to our movie, we can name it and use that name in Action Scripting. Action scripting is to flash what JavaScript is to html, allowing you to "program" your movies.

This lesson will not do anything we haven't done before, but it will allow you to learn a different way of animation in your swf movies. So, let's make some rotating text, turn it into a sprite, then move the sprite with action scripting.

A) Begin as always by opening our html document and starting our php code block

<html>
<body>
<?php


B) We are using action script, so let's make it Flash4 compatible for WebTv users

Ming_useSWFVersion(4);

C) Next, add a font and create some text as usual

$myFont=new SWFFont("../ming-fonts/Chevara.fdb");
$myText1=new SWFText();
$myText1->setFont($myFont);
$myText1->setColor(0,0,255);
$myText1->setHeight(20);
$myText1->moveTo(-$myText1->getWidth("Action Script!")/2, -$myFont->getAscent()/2);
$myText1->addString("Action Script!");

D) Next, we create a sprite and add our text to it

$mySprite=new SWFSprite();
$firstText=$mySprite->add($myText1);

E) We can do just about anything to our sprite we would do to an entire movie, so let's spin our text and add a pause

for($i=0; $i<24; $i++){
$firstText->rotate(15);
$mySprite->nextFrame();
}
for($i=0; $i<24; $i++){
$mySprite->nextFrame();
}

F) Next, we add our sprite to our movie like any other object

$firstSprite=$myMovie->add($mySprite);
$firstSprite->moveTo(-$myText1->getWidth("Action Script!"), 200/2+$myFont->getAscent()/2);

G) In order to manipulate a sprite with action scripting, we need to give it a name

$firstSprite->setName("myWords");

H) Next, we add our action script. Here, we tell out movie to check if our sprite is past the right edge, and if so to move it back to the left edge, if not it will move it to the right by 4 (remember to type this entire line out after CC&Ping on WebTv)

$myMovie->add(new SWFAction("if(/myWords.x>400){/myWords.x=0;}else{/myWords.x+=4;}"));

I) Now we move to the next frame

$myMovie->nextFrame();

J) To insure looping, we add another action script telling our movie to go to the first frame (the first frame is numbered 0) and continue playing, and then move to the next frame

$myMovie->add(new SWFAction("gotoFrame(0); play();"));
$myMovie->nextFrame();

K) Finally, we save our movie, close our php code block, add our object codes, and end our html

$myMovie->save("lesson15.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=400 HEIGHT=200>
<PARAM NAME=movie VALUE="lesson16.swf">
<EMBED src="lesson16.swf" WIDTH=400 HEIGHT=200 TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">
</EMBED>
</OBJECT>
</BODY>
</html>

Though this is just a basic action script example, spend some time learning action script to truly unleash the power in your swf movies!

Code summary for lesson 15:

Result: