I've loaded a SWF into a Master_SWF: how do I control it?
Play around with the SWF below …
When you press the button, the number of the button changes, and at the same time, the number in the square changes the same way.
Here's how it works
…
It's a SWF called "addresstest.swf":
1) On the main timeline of addresstest.swf are 4 identical buttons placed at frames 1,5,10, 15 and marked "1","5","10", "15".
2) Code on each button takes you to the next (i.e., the "5" button sends you to frame 10), and the "15" button takes you back to frame 1 and the "1" button:
on (release) {
gotoAndPlay(1);
}
3) There is a movie clip called inner_mc on the main timeline as an instance named inner. It is constructed just like the main timeline, in 5-frame increments, with the titles changing at each stop, "1", "5", "10", "15".
4) The buttons on the main timeline are double coded, addressing commands to the timeline of inner_mc (instance name "inner"), telling it to go along its timeline at the same rate:
on (release) {
gotoAndPlay(1);
inner.gotoAndPlay(1);
}
Each successive button names the set of frames to follow, both on the main timeline and inner_mc's timeline, so that if you push the button, you'll see the numbers match.
Now Let's Load it into a Master SWF
Click the top button, and the SWF above will load into this new SWF called master.swf.
Here's what you're looking at:
1) The SWF loaded on the HTML page is called master.swf
2) On the main timeline of master.swf is an instance of an empty movie clip —both the symbol and the instance are called empty_mc
3) Again, when you press the "1" button in master.swf, it loads an external swf called addresstest.swf into empty_mc using the loadMovie method:
on (release) {
empty_mc.loadMovie("addresstest.swf");
}
In the master SWF, buttons "2" & "3" control the loaded SWF's timelines, yes, both the main timeline of addresstest.swf, but also the timeline of its internal moveclip inner_mc!
1) Button "2" addresses the loaded SWF addresstest.swf through its host movieclip, empty_mc, and tells it to go to its frame 5 and stop. Note that you don't have to name the loaded external SWF; apparently its timeline is now seen as empty_mc's timeline (which has only 1 frame!). The address starts at master.swf's main timeline, _root, then finds the host MC, empty_mc and tells it what to do.
on (release) {
_root.empty_mc.gotoAndStop(5);
}
2) Button "3" goes further, reaching through the host MC (empty_mc) to the external SWF ("addresstest.swf"), finding on its maintimeline an MC named inner and sending this MC a command to this movieclip to go to frame 10 of its own timeline and stop:
on (release) {
_root.empty_mc.inner.gotoAndStop(10);
}
3) If you play with different combinations of these buttons, you can see how the numbers are changed by the code.
PART II: Using Varibles to Move from Frame to Frame
In the example of a master SWF above, the code just sent the concerned playhead to a single frame. To use the same button to access a series of frames, you need to code it for change. Consider the code we saw before for button "2":
on (release) {
_root.empty_mc.gotoAndStop(5);
}
The playhead is going through the empty_mc to the timeline of addresstest.swf and moving to frame 5, where it's told to stop. To make the playhead go to a series of frames, we need to replace that frame number with a series of others as we press the button. How to do that? Create a variable that will change on each click! Sounds easy, but …
First we save our master.fla as master_2.fla, so we can make changes. Now we can have both files.
OK, here the problem. We need a "stand-in" or variable for the frame number. We also need to add to this number, so that the frame number will change as we press the button, so we need a second variable to be this second number. We need both these variable to be created when the SWF plays the first time, then be reset as needed. Let's make up the name "innerNum" for the first variable — we could call it "bob" or "susan" or whatever, but this name sounds kind of like what its supposed to do. Let's name the second variable "innerPlus" because its adding to this inner number.
The button doesn't work till we press it, so we need to put our code somewhere else.
How about the first frame of the movie, on the ACTIONS layer? Sure, that's the very first code that's read as the SWF begins play, so it's a great choice. Create an actions layer and select the first frame, with and empty keyframe in it. Open its action panel, and write first a variable (written "var") and then set its initial value, like this:
var innerNum;
innerNum = 1;
var innerPlus;
innerPlus = 1;
That's it. We have created a variable by naming it, and then recalled the name to give the variable a value. Since these are created and set from the get-go, we can write code that uses them in everything else. Note that the word "var" turns blue because Flash knows what it is, and "innerNum" stays black, because we made it up.
Now I'm going to go to the middle button, now called button "5" and put code on it. Here's all the code, read it and see if you can see how it works:
on (release) {
innerNum = innerNum + innerPlus;
_root.empty_mc.gotoAndStop(innerNum);
OK, looks pretty much the way it did, but for a couple of things. First, in the familiar line of code, the frame number is replaced with a variable, innerNum. And there's now a line right before it that resets innerNum to be equal to itself added to innerPlus.
Code is read from the top down from the frame event (where we put our first code), so actually, here's the order of things, one after the other:
the enterframe event creates a variable, innerNum
the enterframe event sets a numerical value to innerNum of 1
the enterframe event creates a variable, innerPlus
the enterframe event sets a numerical value to innerPlus of 1
the button event (release) gives a new value to innerNum based on innerPlus
the button event tells the empty_mc to find a frame with the numerical value of innerNum and stop there.
Step # 5 is crucial: it's here that the eventual frame number becomes a variable that will be added to in value using a second variable with each click of the button. See, if the number rises, you can make it read a different higher frame number with each click! For starters, to bump it up over 5 to play the frame 5 movie, we need to add a few units to the frame number. The following code looks at the value of innerNum, and if it finds that innerNum is greater than 1, it resets innerPlus to 5. Since the next click will again add innerNum to innerPlus, the resulting frame number is sure to be over 5.
if (innerNum > 1) {
innerPlus = 5;
}
But … what happens when you get to the biggest value of frame number you've created? Well, you want the variables to return to what they were before they built up. To do this, you have to test how high the number has gotten and make something happen if the number gets too big. So we'll write a conditional statement to have the code see ifinnerNum is greater than 12 — and if it is, the code sets innerNum and innerPlus back to 1 so the build up of frame numbers can start again:
This same code goes on button "6" except, of course, it reaches into the timeline of the inner_mc inside addresstest.swf. Here's the finished product, master_2.swf: