Connecting with External Sources > Sending and loading variables to and from a remote source > Checking for loaded data

 

Checking for loaded data

Each action and method that loads data into a movie (except XMLSocket.send) is asynchronous: the results of the action are returned at an indeterminate time.

Before you can use loaded data in a movie, you must check to see if it has been loaded. For example, you can't load variables and manipulate their values in the same script. In the following script, you can't use the variable lastFrameVisited until you're sure the variable has loaded from the file myData.txt:

loadVariables("myData.txt", 0);
gotoAndPlay(lastFrameVisited);

Each action and method has a specific technique you can use to check data it has loaded. If you use the loadVariables or loadMovie action you can load information into a movie clip target and use the data event of the onClipEvent action to execute a script. If you use the loadVariables action to load the data, the onClipEvent(data) action executes when the last variable is loaded. If you use the loadMovie action to load the data, the onClipEvent(data) action executes each time a fragment of the movie is streamed into the Flash Player.

For example, the following button action loads the variables from the file myData.txt into the movie clip loadTargetMC:

on(release){
	loadVariables("myData.txt", _root.loadTargetMC);
}

An action assigned to the loadTargetMC instance uses the variable lastFrameVisited, which is loaded from the file myData.txt. The following action will execute only after all the variables, including lastFrameVisited, are loaded:

onClipEvent(data) {
	goToAndPlay(lastFrameVisited);
}

If you use the XML.load and XMLSocket.connect methods, you can define a handler that will process the data when it arrives. This handler is a property of the XML or XMLSocket object to which you assign a function you have defined. The handlers are called automatically when the information is received. For the XML object, use XML.onLoad. For the XMLSocket object, use XMLSocket.onConnect.

For more information, see Using the XML object and Using the XMLSocket object.