
Welcome Guest
|
#1
|
||||
|
||||
|
I'm realizing I often have no idea how to work with the asynchronous environment of flash.
I'm trying to load a text file into a variable, with this simple method. Actionscript:
public function loadPlainText(strPath:String):String {
var myLoadVars:LoadVars = new LoadVars();
var myText:String;
myLoadVars.onData = function(src:String) {
trace("src = " + src);
myText = src;
}
myLoadVars.load(strPath);
trace("myText = " + myText);
return myText;
}
trace("LPT = " + loadPlainText("a.txt"));Output goes Code:
myText = undefined LPT = undefined src = *contents of the file* Thanks a lot in advance
Last edited by FlashMove : 03-04-2006 at 03:43 PM |
|
#2
|
||||
|
||||
|
Hello
i think it's impossible that the return statements waits till the data has loaded because it will be imperativly executed . But there are possibilities to get around this.. first-simple you could assign the value for your variable inside the onData Handler if your function is on the Timeline.. if you have declared a varible before you have access to it.. or you call a second function like myLoadVars.onData = setTheValues; see the artikel with a related problem http://www.moock.org/blog/archives/000026.html perhaps its also possible for you that you don't use a function for that hence the loading is a function for itself and you can use it everywhere in your code.. just a suggestion... hope that gives a direction r.fish |
|
#3
|
||||
|
||||
|
I'd like to apoligize to the moderators. I was browsing the forum using several tabs, and thought I was posting this thread in General Flash and not here.
Thanks radicalFish. The example doesn't work for me because I want to use classes when possible, but somehow (?) it made me think and, pretty simply, I could just do something like this in my .fla. Actionscript:
myText = loadPlainText("a.txt");
// The loop running once each frame and my file being relatively small,
// this will probably only run 4-5 times at 30FPS before it's killed
this.onEnterFrame() {
if (myText) {
this.onEnterFrame = null; // deletes the loop
gotoAndPlay(_currentframe + 1);
}
}
stop();
//(and then parse myText or whatever in the next frame)Thanks! Last edited by DenkouNova : 03-04-2006 at 09:40 PM |
|
#4
|
|||
|
|||
|
another possiblity...
I have been working on a very similar problem, and used the following method to make sure that whatever actions I want to perform with the newly aquired data are executed after the data is loaded. I like this method because it keeps the code centralized, but I have still been unable to extend the idea into an organized class structure (more on that later)
Actionscript:
//instantiate the new load vars object
var lvText=new LoadVars();
lvText.onData=function(data):Void{
//make your variable assignment within the onData event handler
myString=data;
// still within this event handler, call a function which does whatever else you want done with the data
parsetext();
}
function parsetext():Void{
//in this case i parse the string with the split method
var wordArray:Array=myString.split(", ");
}this is ok, but i still consider it a workaround. ideally I would like to be able to isolate all this code in a class and be able to comforably load data and manipulate it just as if it had been recieved from an input text box or some other source internal to the swf. If anyone has any insight in this department, it would be much appreciated. |
|
#5
|
||||
|
||||
|
hi
why not writing a class which extends LoadVars. There you can define your own onData, onLoad function handlers, define getter/setter methodes if you want to .. etc.. I ve once written a "XML" class for special needs like this.. r.fish |
«
Previous Thread
|
Next Thread
»
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| XML Issue - Text Node Loading as "NULL" | aura9 | Flash MX ActionScript |
10 | 04-29-2005 06:44 PM |
| External Text File String Into A Variable... | Gramophone | Flash MX |
12 | 06-13-2004 03:38 AM |
| Movie clip needs to be controlled by parameters in text file | tonyc | Flash MX ActionScript |
2 | 02-07-2003 11:36 AM |
| Sporadic loading of variables from a text file | dogzi | Server Side | 3 | 11-29-2000 12:13 PM |
| loading variables from an external text file | stereomike | Flash 5 Actionscript |
6 | 09-05-2000 09:34 PM |

Programming Languages



