|
||||||
|
||||||
Flash Tutorial - Scrolling Dynamically Loaded Text

Free
Flash Tutorial
![]()
With this tutorial you will learn how to scroll dynamically loaded text using the loadVars class from an external Text File. Once you understand the concepts in this tutorial, they can be applied outside text and text boxes to graphics, swf files, and other objects.
In this Flash tutorial I will show you how to load external data from Text file (both plain and HTML) but the principles are the same as other forms of external data (for example PHP, ASP, and XML files)
Cross-Ref: If you are unfamiliar with creating buttons or basic use of functions you might want to look at these tutorials first: Buttons Functions Intro
Note: Scrolling can also be easily done in MX2004 Pro with the use of the UIScrollBar Component (regular MX2004 does not come equipped with this component). But always make note; just the placement of any one component on your stage automatically adds at least 40KB to your flash movie. If you are really trying to trim down your movie, then this would be a better method for you, as this swf file in its entirety takes only 4KB.
Download
the Flash File Int 146a
Example of 3 dynamically loaded pages and their scrollbars
Step One: Setting up the Text Documents

Note: Download the Text files: Here
Or view them individually: page1.txt page2.txt page2.txt
Note: Notepad has nothing to do with Flash. It is a PC's default Text editor. All computers come with a Text Editor as standard. If you are not on a Windows PC you will have an equivalent program somewhere in your computers program menu. You can also create Text files in programs such as Dreamweaver:
Using Dreamweaver to create Text Files.
info=Hello, this is Page One of this flash movie. This information was loaded ...
Note: The Text above is split into sections:
a. Defining a Variable:
info=
b. The Text (know as a String) that will be seen in Flash:
Hello, this is Page One of this flash movie. This information was loaded ...
For Flash to be able to read data in an external file you have to use a variable. The variable is simply a way of defining and naming the text data to be used. The reason it must be named is so that when you are in Flash the ActionScript can refer to it. In this way Flash can remember the text or move it about the Movie. Makes sure no spaces are around the "=" sign
Cross Ref: To learn more about Variables see the tutorial: Variables
Quotes: It is also worth noting that in Flash you always use "quote marks" to define text that you use in variables. If you are loading text from an external file you do not use the "quote Marks".
Or click here to view page2.txt
info=<p>Hello, this is Page Two of this flash movie. This information was, ...
The text above is similar to page1.txt except there are HTML tags in the above text document, such as:
<b></b> (bold),
<i></i> (italic),
<u></u> (underline), ect...HTML tags enable you to format your externally loaded text. For a full list of usable tags in Flash, go here:
http://www.devarticles.com/c/a/Flash/Working-with-Text-and-HTML-in-Flash/4/
Or click here to view page3.txt
info=Hello, this is Page Three of this flash movie. This information was, ...
Step Two: Setting up the Flash Document

Note: For file names do not use spaces or any punctuation. You may use letters, numbers, underscore _ and hyphen - only! You can use capitals if you want.
Step Three: Creating the Text box
(T) 
Note: It is unnecessary to match the Width, Height, X and Y coordinates.

Step Four: Creating the Scroll Button Up



Try it here:
Note: If you need help creating buttons take a look at this tutorial: Buttons

Step Five: Creating the Scroll Button Down




Step Six: Creating the Scroll Bar MC



Step Seven: LoadVars() - ActionScript

ActionScript Explained:
var scrollBarMax:Number = _root.scrollDown._y - _root.scrollTrackbar._y - _root.scrollTrackbar._height
This line determines how far the scroll Bar can move down before touching the scroll down button.
myLV.onLoad = function(success) {
This line defines a function to be called when the handler onLoad is called. Whenever any called text file is completely downloaded from the server and ready to be imported into the movie, this line will call the following function, passing the Boolean variable "success" to indicate whether the text file was found and of the correct type.
loadedInfo.htmlText = myLV.info;
This sets the text in the loadedInfo text box to the text in the text file with the variable name "info". The property htmlText specifies that the text to be imported should be formatted for any html tags.
_root.loadedInfo.scroll = 1;
_root.scrollTrackbar.scrollBar._y = 0;
_root.loadedInfo.mouseWheelEnabled = false
This line will reset the scroll property of the text box and also reset the position of the scroll Bar. The scroll property will be explained in detail later.
scrollCheck();
This line runs the function scrollCheck() which will be later defined. The scrollCheck() function will test to see if the text file is long enough to need scrolling.
loadedInfo.text = "There has been an error loading the requested information. Please contact the Webmaster and report your error."
If the success value from the onLoad handler is returned false, then the text file was either corrupted or was not found. It is always a good idea to put a message just in case an error like this occurs.
Step Eight: Scroll Functions - ActionScript
//defines function called scrollCheck() (with no parameters)
function scrollCheck() {
if (loadedInfo.maxscroll == loadedInfo.scroll) {
scrollUp.enabled = false;
scrollUp._alpha = 50;
scrollDown.enabled = false;
scrollDown._alpha = 50;
scrollTrackbar.scrollBar.enabled = false;
scrollTrackbar.scrollBar._alpha = 50;
} else {
scrollUp.enabled = true;
scrollUp._alpha = 100;
scrollDown.enabled = true;
scrollDown._alpha = 100;
scrollTrackbar.scrollBar.enabled = true;
scrollTrackbar.scrollBar._alpha = 100;
}
}
//defines another function called scrollText() (also with no parameters)
function scrollText() {
_root.onEnterFrame = function() {
//checks to see if the scrollDirection variable has been set to "up" by a button
if (scrollDirection == "up") {
loadedInfo.scroll -= 1;
//checks to see if the scrollDirection variable has been set to "down" by a button
} else if (scrollDirection == "down") {
loadedInfo.scroll += 1;
}
scrollPercent = ((loadedInfo.maxscroll-loadedInfo.scroll) / (loadedInfo.maxscroll-1));
scrollTrackbar.scrollBar._y = scrollBarMax-(scrollBarMax*scrollPercent);
};
}
ActionScript Explained
if (loadedInfo.maxscroll == loadedInfo.scroll) {
This line of ActionScript checks to see if the scrolled position is already the maximum position, meaning the text cannot be scrolled.
Note: Every dynamic text box has a .scroll and .maxscroll property. The scroll property refers to the line that the text box is currently scrolled to (scroll = 1 is what the textbox starts out as, then if you scroll the text box down 1 line then scroll = 2 and so on). The maxscroll property is a read-only property that tells you the maximum value for the scroll property.
scrollButtonUp.enabled = false;
...
scrollTrackbar.scrollBar._alpha = 50;
These next six lines set the properties "enabled" and "_alpha" for the scroll buttons and scroll bar (the instance names will be named later). If "enabled" is false, then any mouse events (on(press,release,rollover...)) will not be called and the button's Up, Over, and Down animations will not play. The _alpha (or transparency) for each button must be set so users will know that the scrolling function is disabled ("grayed-out" buttons usually indicate that they are disabled).
_root.onEnterFrame = function() {
This line defines an enterFrame function on the root timeline.
Note: An enterframe function is a function that is called every time flash updates its frame. This speed is determined by the movie's frame rate (frames per second).
loadedInfo.scroll -= 1;
This line takes the current line that the user has scrolled to and subtracts one from it. This line is the equivalent to:
loadedInfo.scroll = loadedInfo.scroll - 1.
loadedInfo.scroll += 1;
This line takes the current line that the user has scrolled to and add one to it. This line is the equivalent to:
loadedInfo.scroll = loadedInfo.scroll + 1;
scrollPercent = ((loadedInfo.maxscroll-loadedInfo.scroll)/(loadedInfo.maxscroll-1));
This line is a formula for determining the percentage that the text has been scrolled. The variable will be a decimal with a value between 0 and 1.
scrollTrackbar.scrollBar._y = scrollBarMax-(scrollBarMax*scrollPercent);
This line updates the position of the scroll Bar according to the percentage that has been scrolled. This value will be a number between 0 and the scrollBarMax variable.
Step Nine: Scroll Buttons - ActionScript
Step Ten: Scroll Bar - ActionScript
on (releaseOutside) {
this.stopDrag()
delete this.onEnterFrame
}
ActionScript Explained:
this.startDrag(false, 0, 0, 0, _root.scrollBarMax)
This line allows the scroll Bar to be dragged to a confined space. The "false" parameter indicates whether the movie clip's center should "snap" to the mouse when it is clicked. The next three numbers (0, 0, 0) nd the variable (_root.scrollBarMax) refer to the boundary of how far you can drag the movie clip (left, top, right, bottom) with coordinates from the movie clip's axis. Remember that _root.scrollBarMax indicates how far down the scroll Bar should be able to be dragged
Note: You may be wondering why the scrollBar is a movie clip instead of a button; after all, it has mouse events like a button. But it is important to note that startDrag() will only work for movie clips; not buttons. Mouse events, on the other hand, will work with both.
Step Eleven: Creating the Page Links


Step Twelve: Thorough Testing!
Note: Always test your
movie before publishing to check for mistakes, bugs, and any other problems.
In this tutorial you will want to check that:
Warning: This file will not work correctly if the Publish settings are not set to ActionScript 2
Note: Now you must test to make sure the error message works. It is always important to test every aspect of your movies
End of Tutorial
With the aid of this tutorial I hope you have learned to use the scroll property of a text box to scroll its contents, and also how to implement a fully functional scrolling system. Also I hope the use of the LoadVars() will help you to cut down file size in the future by placing as much information as you can outside your flash document.
Why not try out webwasp's new community. Meet new people, find friends in your area: Webwasp Mates & Dates
•
50142 visitors to this page since
Sept 05 •
|
|
|
All material on this site is protected under international copyright © law. DO NOT reproduce any material from this site without written permission. Please ask as permission is often granted.
|