Home > OS >  Loading code from a TXT file on Action Script 3.0
Loading code from a TXT file on Action Script 3.0

Time:09-17

Is there a way to load external code from a .txt file on action script 3? I'd like to put some addChild() codes inside a .txt file to execute on Flash then, for example:

file.txt content

addChild(mc1);
addChild(mc2);

My application does something like this:

fileContents = URLRequest(file.txt);

Now, fileContents has 2 lines of codes I wanna run on Flash, how to run these?

Thanks

CodePudding user response:

You need a function that re-creates the txt files's code dynamically inside AS3.

(1) Extract the code lines into some array of Strings.

(2) Make a function to extract each line into parts.
(eg: extract the command addChild and parameter mc1).

(3) Make a function run_Code where you use the command and parameter.

Example code without array, just a string value, for simplicity...

var myStr = "addChild(mc1);"; //# you read this value from txt file

//# extract using length ( substr )
var myCmd = myStr.substr( 0, myStr.indexOf( "(" ) ); //extract "addChild" (eg: from start until first bracket)

//# extract using positions ( substring ) 
var myParam = myStr.substring( myStr.indexOf("(")  1 ,  myStr.indexOf(")")  ); 

trace( "Command is: "   myCmd   " ... Param is: "   myParam );

run_Code( myCmd, myParam );

function run_Code ( in_command:String , in_param:String  ) :void
{
    //# handle possible commands
    if ( in_command == "addChild" ) { stage.addChild( this[ in_param ] ); }
    
}
  • Related