LOADING
Loading
Hi , welcome back.
LogoutLOGOUT
 
  Lost password?  
Hi
 




ReplyREPLY THREAD
 
Thread Tools Display Modes
  #1  
Old 03-04-2006, 03:21 PM
DenkouNova's Avatar
DenkouNova DenkouNova is offline
Registered User
 
Join Date: Mar 2006
Location: Laval, Quebec, Canada
Posts: 21
Rep Power: 0
DenkouNova is on a distinguished road
Games Loading a text file: loading delay problems

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*
how can I make sure the return statement is executed after onData?

Thanks a lot in advance

Last edited by FlashMove : 03-04-2006 at 03:43 PM
Reply With Quote
  #2  
Old 03-04-2006, 06:45 PM
radicalFish's Avatar
radicalFish radicalFish is offline
Registered User
 
Join Date: Mar 2005
Location: Rennes - Breizh
Posts: 359
Rep Power: 9
radicalFish is on a distinguished road
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
Reply With Quote
  #3  
Old 03-04-2006, 09:22 PM
DenkouNova's Avatar
DenkouNova DenkouNova is offline
Registered User
 
Join Date: Mar 2006
Location: Laval, Quebec, Canada
Posts: 21
Rep Power: 0
DenkouNova is on a distinguished road
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
Reply With Quote
  #4  
Old 03-13-2006, 04:05 AM
flashGordonMX flashGordonMX is offline
Registered User
 
Join Date: Mar 2006
Location: Brooklyn, NY
Posts: 1
Rep Power: 0
flashGordonMX is on a distinguished road
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.
Reply With Quote
  #5  
Old 03-13-2006, 10:42 AM
radicalFish's Avatar
radicalFish radicalFish is offline
Registered User
 
Join Date: Mar 2005
Location: Rennes - Breizh
Posts: 359
Rep Power: 9
radicalFish is on a distinguished road
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
Reply With Quote


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

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




All times are GMT. The time now is 02:33 AM.