Flash ActionScript 3: Reading text from a file -



Flash ActionScript 3: Reading text from a file -

there's problem: want load info text file (named "mytext.txt") flash cs5.5 . contains lines, , want store these lines in array. i've got now:

var myloader:urlloader = new urlloader(new urlrequest("mytext.txt"); var myarray:array = new array(); myloader.addeventlistener(event.complete, loadcomplete(myarray)); function loadcomplete(myarray:array):function { homecoming function(e:event):void { myarray = myloader.data.split("\n"); for(var i:int = 0; < myarray.length; ++i){ trace(myarray[i]); // check if works @ point } } } for(var i:int = 0; < myarray.length; ++i){ trace(myarray[i]); // check if gets modified }

the fact fist part works, loads text file , stores in myarray, , traces it; stores in local version of myarray, doesn't modify reference, for outside of function doesn't trace anything.

i had read arrays passed reference in flash, don't understand why doesn't work.

i appreciate help.

edit

the thing test file, want code in function utilize lot. ideal have static function in class file named "utils", other useful functions. code of "utils.as" file this:

package include { import flash.net.urlrequest; import flash.net.urlloader; import flash.events.event; public class utils { public function utils() { } public static function filetoarray(path:string):array { var linesarray = new array(); // code load file stored in 'path' (the 'path' string // has name of file in it), split '\n' , store every line // in 'linesarray' array. // example: path = "file:////users/pautorrents/desktop/program/text.txt" homecoming linesarray; } // other functions } }

thanks help.

a few things need addressing here.

first, your loop @ end run before load completes, never trace anything. as3 not lock thread when urloader loads, move on rest of code in block while waits load file.

second, it's ugly returning annonumous function result of load finish handler.

here how this:

var myloader:urlloader = new urlloader(new urlrequest("mytext.txt"); var myarray:array = new array(); myloader.addeventlistener(event.complete, loadcomplete, false, 0, true); function loadcomplete(e:event):void{ myarray = myloader.data.split("\n"); for(var i:int = 0; < myarray.length; ++i){ trace(myarray[i]); // check if works @ point } //now move on rest of program/code }

actionscript-3 file flash save

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -