Sending and loading variables
635
Before you can use loaded data in a SWF file, you must check to see whether it has
been loaded. For example, you can’t load variables and manipulate their values in the same
script because the data to manipulate doesn’t exist in the file until it is loaded. In the
following script, you cannot use the variable
lastSiteVisited
until you’re sure that the
variable has loaded from the file myData.txt. In the file myData.txt, you would have text
similar to the following example:
lastSiteVisited=www.macromedia.com
If you used the following code, you could not trace the data that is loading:
loadVariables("myData.txt", 0);
trace(lastSiteVisited); // undefined
Each function or method has a specific technique you can use to check data it has loaded. If
you use loadVariables function or loadMovie function, you can load information
into a movie clip target and use the
onData
handler to execute a script. If you use
loadVariables function to load the data, the
onData
handler executes when the last
variable is loaded. If you use loadMovie function to load the data, the
onData
handler
executes each time a fragment of the SWF file is streamed into Flash Player.
For example, the following ActionScript loads the variables from the file myData.txt into the
movie clip
loadTarget_mc
. An
onData()
handler assigned to the
loadTarget_mc
instance
uses the variable
lastSiteVisited
, which is loaded from the file myData.txt. The following
trace actions appear only after all the variables, including
lastSiteVisited
, are loaded:
this.createEmptyMovieClip("loadTarget_mc", this.getNextHighestDepth());
this.loadTarget_mc.onData = function() {
trace("Data Loaded");
trace(this.lastSiteVisited);
};
loadVariables("myData.txt", this.loadTarget_mc);
If you use the
XML.load()
,
XML.sendAndLoad()
, and
XMLSocket.connect()
methods, you
should define a handler that processes the data when it arrives. This handler is a property of
an XML or XMLSocket object to which you assign a function you defined. The handlers are
called automatically when the information is received. For the XML object, use
XML.onLoad()
or
XML.onData()
. For the XMLSocket object, use
XMLSocket.onConnect()
.
For more information, see
“Using the XML class” on page 654
and
“Using the
XMLSocket class” on page 661
. For more information on using LoadVars to send and load
data that can be processed after the data is received, see
“Using the LoadVars class”
on page 639
.
Summary of Contents for FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Page 1: ...Learning ActionScript 2 0 in Flash...
Page 8: ...8 Contents...
Page 18: ...18 Introduction...
Page 30: ...30 What s New in Flash 8 ActionScript...
Page 66: ...66 Writing and Editing ActionScript 2 0...
Page 328: ...328 Interfaces...
Page 350: ...350 Handling Events...
Page 590: ...590 Creating Interaction with ActionScript...
Page 710: ...710 Understanding Security...
Page 730: ...730 Debugging Applications...
Page 780: ...780 Deprecated Flash 4 operators...
Page 830: ...830 Index...