PDA

View Full Version : Load external text file (name based on SWF movie name)


malino11
11-11-2004, 05:43 PM
Hello,

I am trying to load external text based on the name of the SWF movie. I need to create several SWF files, which will each function as an independent quiz question. Instructors will edit the TXT files without having to work in Flash. If the SWF is named "1" I'd like it to load text dynamically from a text file named "1". This will save me from hard coding what text file will be loaded each time. There could be hundreds of questions total in the end. All of the files will need to exist in one directory.

I managed to trace the name of the SWF file using:

function getFileName(fileUrl) {
var fileName = fileUrl.substr(fileUrl.lastIndexOf("/")+1, fileUrl.length);
return unescape(fileName);
}
trace(this.getFileName(this._url));

Any ideas how I drop the ".SWF" extension and store this file name value as a variable? How can I tell flash to load [variable].txt instead of a known txt file?

Any help would be greatly appreciated. Thank you for your time.

petefs
11-11-2004, 06:06 PM
var txt_filename = this._url.substr(this._url.lastIndexOf("/")+1, this._url.length-4) + ".txt";
then just load unescape(txt_filename);

malino11
11-11-2004, 07:16 PM
Thank you for your prompt reply. The trace is returning the SWF file name with the TXT extension (junk2.swf.txt) for some reason. Any ideas?

Also, how do I specify the loading of a variable rather than an actual file name? I aplogize, I don't have much experience with actionscript. Thank you again.

petefs
11-11-2004, 07:25 PM
hm, really? the second argument given for substr, this._url.length-4 should be 4 characters back from the end of the _url. I don't have flash on my laptop, so I can't check to be sure, but there's no reason what I posted shouldn't work : )

try this and tell me what it traces:
trace(this._url);

then I'll make sure that I give you the proper code for extracting what you want ^_^ as far as loading a variable rather than an actual file name, the variable is a string, so writing this:

load("junk2.txt");

is the same as writing this

var my_var = "junk2.txt";
load(my_var);

No need to apologize, I'm here to help ^_^

malino11
11-11-2004, 07:29 PM
trace(this._url); outputs this:
file:///M|/Users/Quiz%20question/junk2.swf

trace(txt_filename) outputs this:
junk2.swf.txt

what does this mean?: then just load unescape(txt_filename);

Thanks again, I really appreciate it.

petefs
11-11-2004, 08:06 PM
oh duh, sorry -- that's what I mean by braindead : )
substr returns a substring with start & length
substring returns a substring with from & to : )

// get the txt filename
var txt_filename = this._url.substring(this._url.lastIndexOf("/")+1, this._url.length-4) + ".txt";
trace(txt_filename); // "junk2.txt";

// load the txtfile
var myLoadVars = new LoadVars();
myLoadVars.onLoad = function (success) {
if(success) {
// what you want to do with your data
}
else {
trace('error loading data');
}
}
myLoadVars.load(txt_filename);

hopefully there aren't any typos or anything -- let me know how it goes ^_^

malino11
11-12-2004, 03:10 PM
That did it. Thanks! I really appreciate your help.