5. Programming
ActionScript • ActionScript is Flash’s scripting language and is event-based like VB.Net.
• Two types of events each with its own syntax: • Button events pressed, released, rollOver
• Movie Clip events load, enterFrame, mouseMove • ActionScript code is associated with frames, Movie Clips, and buttons.
Controlling frames on the main timeline • Notice that when the application starts, the RobotMovie immediately starts playing
through its different moods. You will write code to stop this. • Go to the main timeline and select the RobotMovie instance. Go to the Property panel
and enter “robot” in the Instance name text field. You do this so you can refer to the instance in the code.
• Add an Actions layer on the main timeline and select the first keyframe on frame 1. • A separate Actions layer is not required, but it helps in finding code.
• Select Window Actions to show the Ac- tions panel. Enter the following Action-
Script code: robot.stop;
• robot is the name of the Movie Clip instance that you are manipulating
and stop is the method that stops the Movie Clip from animating.
16
• The keyframe on the Timeline is marked with an “a” above the dot to indicate that the frame has
code.
• You can also have the RobotMovie symbol start at a particular frame. To do this replace
robot.stop; with robot.gotoAndStop “angry”;
Handling button events • Buttons handle various events such as press, release, rollOver, rollOut, etc.
• You will write code for our buttons to set the mood of our robot when they are clicked. • Add other layers Button Graphics, Buttons, Text, Robot to organize the main timeline
and place the appropriate elements in the layers.
• Select the MoodButton instance. If the Actions panel is not showing, select Window Actions. Enter the following ActionScript code:
onrelease { robot.gotoAndStopangry;
} • Note that the above syntax is specific
to Button instances. • onrelease { ... } is the block
of code that handles the event when the button is clicked. Replacing re-
lease with press, rollOver, rollOut, and other button events
changes the event handled by the code.
17
• Multiple on... blocks can be written for any button. • Proceed to create the other buttons and write similar code to set the mood of our robot
to the other moods. Try handling other button events.
Controlling frames within a movie clip: Animate once • Just as you wrote code to manipulate the main timeline, you can write code to manipu-
late the timeline within a Movie Clip symbol. • Notice that the yucked and jittery mood states are not animated anymore when you
click their corresponding buttons. You will fix this. • Edit the RobotMovie symbol by double-clicking on an instance or the corresponding
symbol in the Library panel. • On the Actions layer, add a keyframe at frame 41.
18
• Select frame 41 and write the code play; in the Actions panel.
• At this point, running the Flash appli- cation and clicking on the yucked but-
ton plays the yucked mood state, but it also plays the jittery mood state.
• On the Actions layer, add keyframe at frame 50.
• Select frame 50 and write the code stop; in the Actions panel. • Now the yucked mood state should play correctly.
• Follow the same instructions for the jittery mood state which lies between frames 51 and 60.
Controlling frames within a movie clip: Looping
• The jittery mood state only plays once when you click the corresponding button. You can have it loop by writing code.
• In the RobotMovie symbol timeline, edit the ActionScript code at frame 60 on the Ac- tions layer.
• Replace the code stop; to gotoAndPlay“jittery”;
How to debug • Use trace“The message goes
here”. This is similar to VB.Net’s Debug.WriteLine“The message
goes here”.
• To show the output of trace, se- lect Window Output. This will show
the Output panel.
19
6. More Programming