Jesse was born and raised in Australia, and now lives in London. He is one of the original founders of http://ActionScript.org, and was formerly a Flash developer, teacher, author and speaker. While Jesse no longer works as a full-time Flash professional, he still enjoys actively participating in the http://ActionScript.org community as time permits.NB: By my own admission this is a bit of a bad way to scroll text, it's good for graphics but not so good for text for various reasons. I've made a new one on text scrolling, go back and you'll see it in the Intermediate section. Also note that this version will scroll forever. If you want it to stop at the top and bottom of your object you need to go through this workaround (please read it all before posting questions on the forums). Cheers - Jesse
Download the source for the above movie here. (Zipped, PC Flash 5 file).
[Rant] There's a rant in the scrolling text field above about why I decided to write this tutorial. I really just used that because I couldn't think of any other text for my field :o) [/Rant]
This tutorial covers scrolling objects including graphic/movie clip symbols and the good old text field. It uses basic getProperty and setProperty principles and masks. If we're talking about text, this tutorial will allow you to scroll dynamically loaded text, such as that loaded from a server-side text file, as it doesn't use motion tweens. The only limitation of this tutorial that I know of is that it, if your text is going to be really long, you need to make the text field larger, but once you do this once, it will work for all really long text strings. So let's get going!
Start by downloading the source. As always it's a Flash 5 file. If, for some reason you can't use my source then create the following structure, the code I will explain later, just make it for now.... (Those of you with the source can skip these dot points):
Some of the code above has been placed over multiple lines for tutorial neatness, in Flash it will probably revert to one line, that's OK.
Now that that's out of the way let's get on to learning how this neat little function works!
Turn the page *bing*...
This is what we achieved in frames 2 and 3 of the fatController clip. Which have this code:
[as]_level0.masker.contentText._y =Note the above code has been put on two lines for tutorial neatness, in Flash it should all be on one line.
So this code first gets the current _y position (which is a numerical pixel value) of the movie clip containing our text field, then adds or subtracts 5 pixels, thus making the text field move up or down. We control whether pixels are added or removed using the variable I've named direction which is set to either 1 or -1. When set to -1 this means that the clip is moved up the screen, because -1 * 5 = -5. So we're adding a negative value, which is like subtracting. That's maths. Remember maths?
But fatController is lazy, so it's first frame is a stop action. If we don't tell it to do something it will sit around doing nothing. That's where our button auctions come in. Take the up button for example. It has the following code:
[as]on (rollOver) {So, on roll over, this button sets our direction variable to -1, so our text will move up the screen. Then it tells the fatController to get a move on! It plays fatController at Frame 2 which has our Y position incrimentation code, as discussed above. Frame 3 does the same and loops back to Frame 2. This loop continues and causes regular 5 pixels movements in the Y position of our clip. This gives the scrolling effect.
The second part of the button code tells the fatController to take a rest when the users moves the mouse off the up button. fatController then goes back to it's first frame, which has a stop instance, for a rest. Note that I've put a whole lot of events in the second on() tag. This is so our code wont stuff up if a user moves over the button, clicks and then moves out, or clicks and drags out, or some other weird, unpredictable scenario. This just makes our code function better in all environments, the only event you really need to include is rollOut.
Finally, the text field is dynamic so we can change the text easily. I've set the initial value in the first keyframe of the main stage, but you could just as easily use loadVariablesNUM() to load in a text file whose text you wish to show in the text field.
Not too hard hey? If you have questions post the on the forums please, rather than emailing me directly.
| 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. |