Moving from Scene to Scene
Be careful with scenes: Most animators, and Macromedia as well, don't recommend using scenes except with long animations. We're discussing it here only because you should know how to do it.
Let's say our assignment is to create a Flash version of a PowerPoint presentation. It's for Buzzkill Industries, a young, edgy company that makes safety equipment. Someone else has created the slides for the presentation, and it's our job to make sure the presenter can move from slide to slide. For the purposes of this tutorial, we'll only deal with the first three slides of the presentation, shown in Figure 34.
Figure 34 All three slides of the Buzzkill presentation
Open the file chapter3/buzzkill1.fla.
Notice that there are three scenesone for each slide. Each scene has one frame action on its last frame: stop().
Click on the last frame in the actions layer in each scene, and then open the Actions panel to see the ActionScript. The only ActionScript you'll see is stop(). This stop method halts the playhead in its tracks. That is, it stops the Flash movie from playing further.
Go ahead and play with the movie, using Control Test Movie.
NOTE
Always test your movies with Control Test Movie, since that's the only way you can properly test your actions.
Notice that there's no way to go beyond the first scene/slide.
Close the window with the movie playing.
Go to the last frame in Scene 1 of the movie (frame 60) on the button layer.
Insert Keyframe.
Window Library.
Drag the Forward Button symbol onto the empty button layer. Make sure you're still on frame 60. Position the button wherever you want.
Click on the button.
Open the Actions panel.
Enter the following code:
on(release) { gotoAndPlay("Scene 2","slide2"); }
Control Test Movie. Once the movie is playing, pressing the button should send you to the next scene.
Let's look at this code in detail. The first thing you see is the on(release); on is what's known as an event handler. If something happens, like the user pressing down the mouse button or hitting the keyboard, that's called an event. An event handler is some code that is executed when an event happens. For buttons in Flash, their event handler is the on(event) function. There are about a dozen events (for a complete list, see Appendix A, "ActionScript Reference"), but the only one we're dealing with is release, which is short for "when the user presses the mouse button and then releases it."
Once the user releases the mouse button, everything between the curly braces that follow the on(release) is executed. As it happens, there's only one line of code: gotoAndPlay("Scene 2", "slide2"). As you might have guessed, this command tells the playhead to jump to Scene 2 and start playing from the frame that has the label "slide2". In this case, frame 1 holds the "slide2" label.
Unfortunately, Flash isn't smart enough to look through all the scenes in your movie and find the right frame if you've labeled it, so gotoAndPlay("slide2") wouldn't have worked. If you direct Flash to a frame label that doesn't exist, it'll start playing at frame 1.
Here's another way to do it:
on(release) { gotoAndPlay("Scene 2", 1); }
In this case, Flash goes to Scene 2 and starts playing at the first frame. I recommend using frame labelsit lets you move stuff around without having to keep track of frame numbers.
NOTE
Labels and actions do not have to go on their own layers. However, it's a good way to stay organized, especially as your movies get more and more complex. I recommend doing it, even if there's only one frame label or only one frame action. I also strongly recommend always using frame labelsdon't use frame numbers. Your ActionScript is less likely to have bugs down the road if you use frame labels.
Now let's add the two buttons on the second scene/slide.
Open up Scene 2.
Open up the Library if it isn't already open.
Go to the last frame in the movie (40) on the empty button layer.
Insert Keyframe.
Drag the Backward button and the Forward button to the stage and place them wherever you like.
Click on the Forward button.
Open up the Actions panel if it isn't open.
Enter the following code:
on(release) { gotoAndPlay("Scene 3","slide3"); }
Click on the Backward button.
Enter the following code:
on(release) { gotoAndPlay("Scene 1","slide1"); }
Save the file and Control Test Movie.
For the final step, let's add the last button to the final slide.
Open up Scene 3.
Open up the Library (Window Library) if it isn't already open.
Go to the last frame in Scene 3 on the button layer (frame 45).
Drag the Backward button to the stage and place it wherever you like.
Click on the Backward button.
Open up the Actions panel if it isn't open.
Enter the following code:
on(release) { gotoAndPlay("Scene 2","slide2"); }
Control Test Movie!
Now you can actually move from scene to scene inside your movie, and you learned something about buttons and event handlers along the way! Now let's make this a little more complex: It turns out that, since they are used to using PowerPoint, the presenters forget that they have to press buttons on the screen to move from scene to scene. They want to be able to press the space bar and right arrow to move forward and the left arrow to move back, just as in PowerPoint.
This isn't a problem at all. All we're doing here is adding events (the user is pressing keys on the keyboard), so we can modify the buttons we already have to deal with those events.
Go back to the Forward Button on frame 60 of Scene 1.
Click on the button and open its Actions panel.
Add the following code:
on(release, keyPress "<space>") { gotoAndPlay("Scene 2", "slide2"); }
Control Test Movie.
The new event we have here is keyPress, and part of using keyPress is saying immediately afterwards which key we're looking for. By using the <> brackets and spelling out space, we've made this code easier to read than if it were keyPress " ".
Now let's add the right arrow:
Enter the following code:
on(release, keyPress "<space>", keyPress "<right>") { gotoAndPlay("Scene 2", "slide2"); }
Control Test Movie.
Whoops! You received an error. Flash doesn't like to have two of the same events in the same on() statement. Even though our two keyPress events are for different keys, they still count as the same kind of event. So we have to rewrite our code slightly:
on(release, keyPress "<space>") { gotoAndPlay("Scene 2", "slide2"); } on(keyPress "<right>") { gotoAndPlay("Scene 2", "slide2"); }
Test the movie again to make sure it's working correctly.
Now that we have the first button working, getting the others to work won't take much extra effort. Here's the code for them:
Scene 2 Backward Button
// This button only needs one function, since only // one key can be used to go backwards, as opposed to // two keys to go forward. on(release, keyPress "<left>") { gotoAndPlay("Scene 1", "slide1"); }
Scene 2 Forward Button
on(release, keyPress "<space>") { gotoAndPlay("Scene 3", "slide3"); } on(keyPress "<right>") { gotoAndPlay("Scene 3", "slide3"); }
Scene 3 Backward Button
on(release, keyPress "<left>") { gotoAndPlay("Scene 2", "slide2"); }
To see the final movie, check out buzzkill2.fla.
Comments
Did you notice we introduced one new element? For the Scene 2 Backward Button, there are a few lines that start with two slashes. Those two slashes indicate that the rest of the line is a comment, which Flash completely ignores. Comments are only for programmers, not for the computer. Using comments appropriately is called documenting your code. Documenting your code is important, since at some point you'll have to fix your own old code when you might not remember how it works anymore, or someone else might have to work on your code. Programmers who document their code well are happy programmers.
on(event)
As a final note, here are all of the events that the on event handler can recognize:
- press
- release
- releaseOutside
- rollOver
- rollOut
- dragOver
- dragOut
- keyPress
These are discussed in greater detail later in the book and in Appendix A.