Now we need to create our function which will handle directions of where to scroll to and how. Return tot he main timeline by clicking the 'Scene 1' above and to the left of the timeline (above the Scripts Layer).
Drag one instance of your fully completed 'Bar' movie clip onto the stage from the Library (Ctrl L to pop up the library). If your mask is working correctly you should see only the first scene worth of content, not the entire length of the 'content' graphic symbol. Give this instance of the bar symbol an instance name of 'bar'. Now create a button and put a few copies of it on the stage. Add these actions to your buttons:
on (release) {
/bar:target =
targetScene;
director ();
}
Where '
targetScene' is the content-scene you want that button to scroll to. For example, if I want a button to scroll to content scene
5 I would enter:
on (release) {
/bar:target =
5;
director ();
}
Actions Evaluation: What we're doing and how.
Line 1 ? Says "Do these actions when the button is released"
Line 2 ? Sets the variable "target" within the movie clip instance "bar" for your chosen number, (5 in the example above).
Line 3 ? Calls our director() function, which we'll make below.
You should have one button for each content scene you have. So if your content has 8 segments, you should have 8 buttons on the stage each with the appropriate code.
Now double click frame 1 of the main timeline to open the Actions Inspector. Enter expert mode using the Menu arrow on the right side of the Actions Inspector (or Ctrl E), and paste in the following script:
stop ();
// set startup variables - we don't want it scrolling away as soon as the file opens!
/bar:target = 1;
/bar:direction = "f";
// our 'director' function, which will control movements
function director () {
if (/bar:target</bar:current) {
// if we're going backwards then, set direction to backwards!
/bar:direction = "b";
} else if (/bar:target>/bar:current) {
// if we're going forwards then, set direction to forwards!
/bar:direction = "f";
} else if (/bar:target==/bar:current) {
/bar:direction = "null";
}
goto = (String(/bar:direction+/bar:current));
bar.gotoAndPlay(goto);
}
Actions Evaluation: What we're doing and how.
Line 1 ? stops the movie.
Line 2 ? Comment
Line 3 ? Sets variable target to 1. Meaning we want to go to and stop content-scene 1 when the movie first loads.
Line 4 ? Sets variable direction to "f". Our
director function interprets this to mean "We are moving forwards ? scrolling from 1 through 5".
Line 6 ? Define custom function:
director()
Line 7 ? Conditional statement: tests if the target content-scene is less than the current content-scene. ("Are we moving backwards from 5 through 1?")
Line 9 ? Since the target content-scene is less than the current target-scene, we are moving
backwards, so set direction to "
b"
Line 10 - Conditional statement: tests if the current content-scene is less than the target content-scene. ("Are we moving forwards from 1 through 5?")
Line 12 ? We are moving
forwards, so set direction to "
f"
Line 13 ? Conditional statement: Is the target content-scene the same as the current content-scene? "Are we staying still?"
Line 14 ? If we're staying still, we have no direction. Set direction to "null"
Line 16 ? set variable "goto" to the String (text value) derived from taking the direction and adding the current content-scene number.
Line 17 ? Tell the "bar' instance of the "bar" movie clip symbol to go to and play the label stored in variable "goto" if such a label exists.
OK so let's take an example scenario and run through it.
Say we're currently at content-scene 4 and the user decides to go to content scene 2.
Our function will do the following things:
- Figure out that 2 is less than 4 so we're going backwards.
- Set the direction to "b" to indicate backwards
- Tell the 'bar' movie clip to go to and play label "b4" (which scrolls backwards from 4 until it reaches it's target frame, which is 2 in this case).
And that's it! You now have a scrolling navigational thing. This version will even re-route in mid tween if you press another button. For example, press the 4 button, then wait for it to scroll past 2 and press the 1 button and it will scroll back without going all the way to 4!
It's a bit messy trying to write out layout intensive tutorials like this so I hope you found it easy to comprehend. I do my best! Let me know if there's some way I can make it clearer, or if you find any errors, etc.
| Jesse Stratford [email:jessestratford@actionscript.org] is the Co-Master of ActionScript.org and a freelance Flash developer and teacher. He is based in Australia and enjoys all things Flash. NB: If you have comments or feedback please feel free to email me, but please do not email me Flash questions; the forums are provided for that purpose and you will get a faster answer by posting you question there. |
 |
If you have found this tutorial helpful, I hope that you will take 30 seconds to visit The Hunger Site where, with just one click you can make a free donation of food to a starving person in a third-world country. We do not benefit financially from this action; it is purely an act of charity. |
 |
| This tutorial is protected by International Intellectual Property Rights laws and may not be reproduced or redistributed in full or part, without the prior written consent of the author. Unauthorized reproduction of this tutorial or its contents may result in prosecution. I've worked hard on this tutorial, please don't steal it. |