Home » ActionScript 3.0, Flash CS5

AS3 + Flash Loading Variables from a Text File

6 January 2012 One Comment
Share on Facebook

With as3 you can load variable from a text file into flash, here the tutorial explains how to achive with actionscript 3.0 Suppose we need use data which changes periodically can be edited in notepad and can be loaded in to Flash dynamically.

Difficulty:

  • Basic – Intermediate

flash as3 source file

Setup text file with variables:

Before starting with actionscript or Flash first we need to setup the text file with variables. Text file should include name and value pair (name=value) seperated by ampersand ( & ).

Example of name and value pair:

name1=value1&name2=value2

Note: we can have any set of name and value pair in notepad or text file.

Check out the sample notepad image below, Find the same sample text file with source.

as3 load variable text files

[ Example of name & value pair ]

Simple code:  loading text file variables in as3

Below is a simple code to load the text file data into Flash.

import flash.events.*;
import flash.net.*;

var urlLoader:URLLoader = new URLLoader( );

urlLoader.addEventListener( IOErrorEvent.IO_ERROR, handleIOError );
urlLoader.addEventListener( HTTPStatusEvent.HTTP_STATUS, handleHttpStatus );
urlLoader.addEventListener( SecurityErrorEvent.SECURITY_ERROR,  handleSecurityError );
urlLoader.addEventListener( Event.COMPLETE, handleComplete );

urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;  

urlLoader.load( new URLRequest( "dataFile.txt" ) );

function handleIOError( event:IOErrorEvent ):void
{
	trace( "IO error: " + event.text );
}
function handleHttpStatus( event:HTTPStatusEvent ):void
{
	trace( "HTTP Status = " + event.status );
}
function handleSecurityError( event:SecurityErrorEvent ):void
{
	trace( "Security Error: " + event.text );
}
function handleComplete( event:Event ):void
{
	trace( "Data loaded" );
	trace( "name1 :: " + urlLoader.data.name1 );
	trace( "name2 :: " + urlLoader.data.name2 );
}

AS3 loading variable code Explanation:

import flash.events.*;
import flash.net.*;

import necessary class files.

var urlLoader:URLLoader = new URLLoader( );
urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
urlLoader.load( new URLRequest( "dataFile.txt" ) );
 

We have declared new object of URLLoader Class and loading the text file via urlLoader.load(), then setting the dataFormat of URLLoader Object as URLLoaderDataFormat.VARIABLES;

URLLoaderDataFormat.VARIABLES

This indicates that we are about to load variables from external file.

Reading the variables finally

Once we load the external text file via urlLoader.load() function, flash player attempts to load the file and triggers “Event.COMPLETE” on load complete. Other Events we have added are,

httpStatus( HTTPStatusEvent )

Triggered when Flash Player can detect the status code for a failed HTTP request when attempting to load data.

ioError( IOErrorEvent )

Triggered when the Flash Player encounters a fatal error similar to file not found, that results in an aborted download.

securityError( SecurityErrorEvent )

Triggered when text file is loaded from a domain that resides outside of the security sandbox.

trace( "name1 :: " + urlLoader.data.name1 );
trace( "name2 :: " + urlLoader.data.name2 );

Above code traces the values fetched from the notepad and displayed in output window.

Note:name1 and name2 are the variable name in text file.

Conclusion:

We have seen how to load a simple variables from text file via Flash as3. Please find the source files attached.

GET THE UPDATES VIA EMAIL

We don't share your email anywhere, grab our rss via feedburner


  • http://www.wefdc.com/?p=3105 [业界动态] Flash平台酷成员 | Flash开发者大会

    [...] Saravanan发布了两个教程,第一个介绍了Twitter API应用oAuth。第二个介绍了如何通过文本文件加载变量。 这是一个使用actionscripte和FLASH的3D旋转地球示例代码。 [...]