• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Lesson 8. Flash Site Architecture > Controlling Loaded Movie Timelines from the...

Controlling Loaded Movie Timelines from the Main Movie

The final task in this lesson is to add functionality to the Restart Tutorial button. Specifically, the Restart Tutorial button should be invisible when no movie is loaded, visible when either movie is loaded, and scripted so that it can return either loaded movie to its frame 1. Accomplishing this task is easier than you think. In fact, at this point it is mostly a retread of things covered in the previous two lessons. I included it here to give you extra practice, drive home a point (more on that later), and let you do a little showboating.

1.
Select the Restart Tutorial button, and add the following script to it:

							on (release) {
							container.gotoAndStop(1);
							}
						

The button is targeting a built-in method of the movie clip class (gotoAndStop()). It personalizes the method by telling Flash which movie clip (the one called container) and by telling which frame of the movie clip's timeline to use as its destination (frame 1).

It's interesting to note that this script doesn't care that sometimes the movie loaded into the container movie clip is sim_dsn.swf, and other times it is sim_definesite.swf. Either movie can be found in the container, and both have a frame 1 so the rest is academic, as far as this script is concerned. This demonstrates that for practical purposes, loaded movies become their movie clips, and everything outside the movie clip can still control the loaded movies. This is a great example of object-oriented programming. No matter what you put in the container movie clip, now or later, this script will still control it. However, if this script targeted a frame label, such as intro, instead of frame 1, then it wouldn't work if the loaded movie didn't have a frame labeled intro. Since all movies have at least one frame, this script is unbreakable.

The container movie clip doesn't always have a movie loaded into it. You might wonder what happens with this script when no movie is loaded into it (remember that none is by default). Now, the empty container movie clip is still on the timeline, and it has an empty frame 1, so the script on the button isn't technically broken, but it doesn't appear to do anything unless the user has loaded a movie and advanced beyond frame 1. It would be good to hide this button when no movie is loaded.

2.
Select the Restart Tutorial button, and in the Property inspector, give it an instance name of restart_btn.

In Flash 5, only movie clips get instance names; but in Flash MX, movie clips, buttons, dynamic and input text objects, and components can all have instance names, which allows you to control their properties just as you would a movie clip. And the movie clip property that should come to mind now is, of course, our old friend _visible.

3.
Select the Creating a DSN Button and add restart_btn._visible=1; beneath the definesiteToggle=false; line. Then beneath the dsnToggle=false; line, add restart_btn._visible=0;. Repeat this process on the Defining a Dynamic Site button.

Once you have completed this step, you have the final version of these two scripts. At 12 lines long, they're not the most complex ActionScript ever written, but the way you built them in this lesson is typical of how scripts are written: one step at a time, adding one piece of functionality at a time, with lots of testing and trial and error as they are expanded to solve multiple problems. Even experienced programmers script this way. When it comes to scripting, first imagine what you want, outline it in pseudocode, and then build it piece by piece, testing, breaking, revising, and improving it until you are satisfied.

The final version of the Defining a Dynamic Site button is as follows:

							on (release) {
							if(definesiteToggle==false) {
							loadMovie("sim_definesite.swf","container");
							definesiteToggle=true;
							dsnToggle=false;
							restart_btn._visible=1;
							} else {
							unloadMovie("container");
							definesiteToggle=false;
							restart_btn._visible=0;
							}
							}
						

We are not finished, however. You have added functionality to turn the Restart Tutorial button on when a movie is loaded, and turn it back off when a movie is unloaded, but you haven't done anything to hide it in the first place.

4.
In frame 1 of the actions layer, add the following action to the end of the list:

							restart_btn._visible=0;
						

This will hide the button initially. When the user clicks one of the tutorial buttons, the visibility of the button will be toggled on. When the movie is unloaded, the restart button's visibility will be toggled back off.

The final version of the frame action script is as follows:

							stop();
							dsnToggle=false;
							definesiteToggle=false;
							restart_btn._visible=0;
						

When you are finished, save the file and test the movie, trying every possible combination of button presses to verify that you can't break the scripts.

Looking back over the completed lesson, you should be able to see how different this particular file is from an HTML page. You enabled users to modify or control several kinds of objects, including movie clips, buttons, and loaded movies. From a single button, you controlled three different objects—the variable on the main timeline, the button's visibility property, and the external SWFs that you loaded into the container movie clip—and what's more, you controlled them differently depending on the context, thanks to the if…else clause. As you add more objects to your movies, each with its own interactive capabilities, you empower the user to control the media in a nonlinear, fully dynamic, personalized experience.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint