Data
Data can come in many formats. It can be a text file, XML file located on a file system, or dynamic data/XML generated by a server at run-time.
In ActionScript you can load data from external sources into an SWF file. You can also send data from an SWF file for processing by an application server (such as Microsoft IIS, Macromedia ColdFusion MX, Macromedia JRun, IBM Websphere, BEA Weblogic, and so on) or another type of server-side script, such as ASP, PHP or Perl.
Macromedia Flash Player can send and load data over HTTP or HTTPS, or load from a local text file or XML file. You can also create persistent TCP/IP socket connections for real time messaging applications.
The following are the different ways that you can exchange data that are available in ActionScript.
getURL(), loadVariables(), loadVariablesNum(), loadMovie(), and loadMovieNum() methods
LoadVars Class
MovieClipLoader class
XML Class
XMLSocket Class The methods listed as the title of this section use the HTTP or HTTPS protocol to send information in URL-encoded format.
getURL(): This method loads a document from a specific URL into a window or passes variables to another application at a defined URL. This method can used when you want to load a different URL onto the existing one. You also have the option of passing the data to a different URL using http POST and GET methods.
Example:
//opens http://www.devarticles.comin a blank windowgetURL("http://www.devarticles.com", "_blank");//sends the variables from flash to http://www.devarticles.com
using POST methodgetURL("http://www.devarticles.com", "_blank", ”POST”);
loadVariables() , loadVariablesNum():These methods reads data from any external file, such as a text file or text generated by any server-side scripting technology such as ColdFusion, CGI script, ASP, PHP, Perl, and so on, and set the values for variables in a Flash.
The main difference between the loadVariables() method and the loadVariablesNum() method is that loadVariables() sets the values for variables in a target movie clip, while loadVariablesNum() sets the values for variables in a level. You can also use this method to update variables in the active SWF file with new values.
The text in the text files, or the text generated by the server-side script, should be in the standard MIME format application/x-www-form-urlencoded (a standard format used by CGI scripts). Any number of variables can be specified, as in the string shown below.
name=Jhon&age=22&zip=534235
Example:
// Variables from data.txt will be loaded into target_mc movie cliploadVariables("data.txt", target_mc);// Variables from data.txt will be loaded into level 1loadVariablesNum("data.txt", 1);
You can also use the movie clip’s loadVariables() method to load data directly into the movie clip.
Ex: target_mc. loadVariables ("data.txt");
loadMovie() , loadMovieNum(): These methods let you load external SWF files and JPEG files into the existing SWF file and are similar to loadVariables() and loadVariablesNum() methods. The difference between loadMovie() and loadMovieNum() is also the same, target and level.
Example:
// Loads play.swf file into target_mc movie clip
loadMovie("play.swf", target_mc);
// Loads play.jpg image into level 1
loadMovieNum("play.jpg", 1);
You can also use the movie clip’s loadMovie() method to load external SWF files or JPEG images into the movie clip directly.
Ex: target_mc.loadMovie("play.jpg");
The LoadVars Class can be used in lieu of loadVariables() and loadVariablesNum() methods. It has three methods that use the HTTP or HTTPS protocol to send and load information in URL-encoded format.
This class was introduced in Flash Player 6 to provide a cleaner, much more object-oriented interface for the common task of exchanging data with a server or text files.
When compared to loadVariables() and loadVariablesNum() methods, LoadVars class has many advantages.
Example:
The following example instantiates a LoadVars object, assigns a couple of variables to it and sends those variables to a PHP page with http POST method.
var sample_lv:LoadVars = new LoadVars();sample _lv.name = “Jhon”;sample _lv.age = “36”;sample _lv.send("setid.php", "_blank", "POST");
The MovieClipLoader Class can be used in lieu of the loadMovie() method.
This class lets you implement listener call backs that provide status information while SWF or JPEG files are being loaded into movie clips.
Instead of using loadMovie()or MovieClip.loadMovie() methods, you need to use MovieClipLoader.loadClip() to use the features of the MovieClipLoader class.
The following events will take place in the order listed when you use MovieClipLoader Class:
MovieClipLoader.onLoadStart– This event listener will fire when the first bytes of the SWF or JPEG file are downloaded.
MovieClipLoader.onLoadProgress– This event listener will continuously fire during the loading process, and can be used to show the loading progress to the users. During the load process you can call MovieClipLoader.getProgress()at any time to know the progress of the loading.
MovieClipLoader.onLoadComplete– This event listener will fire when the entire file has been downloaded.
MovieClipLoader.onLoadInit– This event listener will fire after the downloaded file’s first frame actions have been executed. This will be helpful for performing any actions on the loaded file. Until this event listener fires, you cannot perform any actions on the loaded file.
Note: If there is any problem loading the file then MovieClipLoader.onLoadErrorevent listener will fire and return the error code.
Now let us see an example of how to use the MovieClipLoader class in action.
Example:
// Instantiate MovieClipLoader Class
var adloader:MovieClipLoader = new MovieClipLoader();
// use loadClip to load SWF file into a movie clip header_mc
adloader.loadClip("mainad.swf","header_mc");
// Add an onLoadStart event listener to show the progress of the loading
adloader.onLoadStart = function (targetMC){
var loadProgress:Object = adloader.getProgress(targetMC);
var loaded:Number = loadProgress.bytesLoaded;
var total:Number = loadProgress.bytesTotal;
adprogress_txt.text = loaded+" of "+ total+" bytes loaded";
}
// Add an onLoadComplete event listener to clear the loading information
adloader.onLoadComplete = function (targetMC){
adprogress_txt.text = "";
}
// Add an onLoadError event listener to trace the load errors
adloader.onLoadError = function (targetMC, errorCode){
adprogress_txt.text = "ERRORCODE:" + errorCode + " failed to load";
}
Extensible Mark-up Language (XML) is the future standard for exchanging structured data in Web based applications. Data in Flash can be integrated with servers that use XML technology to build sophisticated and effective, rich Internet applications.
ActionScript in Flash has a built-in XML class whose properties and methods can be used to load, parse, send, build, and manipulate XML document trees.
The methods load(), send(), and sendAndLoad() of the XML Class use HTTP or HTTPS protocol to send and load information as XML.
The load() method downloads XML from a URL and places it in an ActionScript XML object.
The send() method encodes the XML object into an XML document and sends it to a specified URL using the POST method. If specified, a browser window displays returned data.
The sendAndLoad() method sends an XML object to a URL. Any returned information is placed in an ActionScript XML object.
Apart from the above methods, the ActionScript XML Class has several other methods to let you structure the XML data in Flash to send to a server, and also to manipulate and interpret downloaded XML data from the server or file system. Methods and properties such as childNodes(), hasChildNodes(), attributes, firstChild, createElement(), and so forth let you parse and create XML files at run-time.
Now let us see a simple example for loading an external XML file into flash.
Example:
// create a new XML objectvar sample_xml:XML = new XML();// load the XML into the sample_xml objectsample_xml.load("sample.xml");// set the ignoreWhite property to true to ignore white spaces and
new line characters in the XML document (default value is false)sample_xml.ignoreWhite = true;// onLoad event handler function which fires after the loading of
XML file is completesample_xml.onLoad = function(success) {// you can perform actions on the loaded XML here};
XMLSocket class in ActionScript lets you open a continuous connection with a server. A socket connection lets the server publish, or push, information to the client as soon as that information is available. The data is sent over the socket connection as one string and should be formatted as XML. You can use the XML class to structure the data.
The methods of XMLSocket Class create and use TCP/IP socket connections to send and load information as XML.
You can use the connect()and send()methods of the XMLSocket class to transfer XML to and from a server over a socket connection. The connect()method establishes a socket connection with a Web server port. The send()method passes an XML object to the server specified in the socket connection.
Now let us see an example that shows how to use the XMLSocket Class.
Example:
// Create XMLSocket objectvar sample_socket:XMLSocket = new XMLSocket();// Connects to localhost server with port number 1024sample_socket.connect("localhost", 1024);// Displays connection status informationsample_socket.onConnect = function(status) {if (status) {conn_txt.text = "connection successful";} else {conn_txt.text = "no connection made";}};// Function that creates data to send it to the server using
send() methodfunction sendData() {var data:XML = new XML();var send = data.createElement("user");send.attributes.id = "asp123";data.appendChild(send);sample_socket.send(data);}// onData event handler function to track the data returned from
socket connection and perform actions based on that datasample_socket.onData = function(msg:String):Void {trace(msg);};
Note:The XMLSocket.connect()method can connect only to TCP port numbers greater than or equal to 1024. Port numbers below 1024 are often used by system services such as FTP, Telnet, and HTTP, so XMLSocket objects are barred from these ports for security reasons.