views:

53

answers:

2

Hi,

I have an external log file which name changes each session, with the format XXXXX.log

I need to load it inside a swf to show its data, but each time the log´s name is different, I need to open the .fla, changing the name of the file and then republishing the swf.

So I have made a simple script to load another .txt, to type manually in it the 5 number of the log´s name and load it externally inside the swf:

var logNumLoader:URLLoader = new URLLoader();
logNumLoader.dataFormat=URLLoaderDataFormat.VARIABLES;
logNumLoader.addEventListener(Event.COMPLETE, loadedLogNum);
logNumLoader.load(new URLRequest("infoLogNum.txt"));

function loadedLogNum(e:Event):void {

         trace(e.target.data.logNum); //  HOW TO GET THE VALUE OF logNum... *(see below)

        }

The "infoLogNum.txt" contains just a variable, which is the manually typed name of the .log, like:

logNum=12345

This script works perfect, as it outputs the variable value, in this case "12345".

But I need this "logNum" value to be passed inside another function that loads and opens the XXXXX.log (see in the code at the bottom):

...

var tracksLoader:URLLoader = new URLLoader();
tracksLoader.addEventListener(Event.COMPLETE,onTracksLoaded);
tracksLoader.addEventListener(IOErrorEvent.IO_ERROR,onTracksError);
tracksLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onTracksError);

loadTracks();

function onTracksLoaded(e:Event):void {
    trace("onTracksLoaded");
    parseTracks(tracksLoader.data); 
    reload.start(); 
}

function onTimer(event:TimerEvent):void{ 
    loadTracks();
}

function onTracksError(e:Event):void {
    trace("onTracksError", e);
    reload.start();
}

function loadTracks():void {
    tracksLoader.load(new URLRequest(logNum+".log")); // (*) ...AND SET THE logNum VALUE HERE 
}

While I cannot assign the value of logNum to the last function, the .log cannot be opened.

I have no clue on how to make it work. Any ideas, please?

TIA.

A: 

Perhaps something like this:

function loadedLogNum(e:Event):void {
  var logNum:String = e.target.data.split("=")[1]; // returns "12345"
  tracksLoader.load(new URLRequest(logNum + ".log"));
}
JohnWinkelman
@JohnWinkelman. I couldn´t make it work either. I have updated the rest of my code. There is another URLLoader for the log file. The first one works ok as variable, I think changing it as string is not the solution needed. Thanks.
Pablo
A: 

What is happening on your code is that youa re running loadTracks() before the code has enough time to load the log number. Second you can assign the logNum to a local variable that then can be used in your loadTracks() function.

logNumLoader.load(new URLRequest("infoLogNum.txt"));

//New local variable that will hold the logNum;
var _logNum : String ;

function loadedLogNum(e:Event):void {

trace(e.target.data.logNum); //  HOW TO GET THE VALUE OF logNum... *(see below)
// Once infoLogNum is loaded, you can assign the value to the local variable         
_logNum = e.target.data.logNum;
// Now you can load your tracks.
loadTracks();
   }

//Down in your loadTracks you can use the local logNum.
function loadTracks():void {
    tracksLoader.load(new URLRequest(_logNum+".log")); // (*) ...AND SET THE logNum VALUE HERE 
}
Helmut Granda
If you want to take your script further you can edit your application so that it takes the parameters from the address bar.http://www.helmutgranda.com/?logNum=12345Now you can dynamically read logs without having to edit a text file and you might even be able to send the address to other people.
Helmut Granda
another thing that I forgot to comment is that in fact you can pass the parameter to load tracks.after the loadedLogNum event you can do the followingloadTracks(e.target.data.logNum); //this is dirty but just to prove the pointin loadTracks.function loadTracks(_logNum : String):void {tracksLoader.load(new URLRequest(_logNum+".log")); // (*) ...AND SET THE logNum VALUE HERE }
Helmut Granda
@Helmut Granda. Thank you so much, it worked!! About taking the parameters from the address bar, this script loads the .log from another application (it´s for local use), so I think that option is not needed. Thank you again!
Pablo