Skip to main content
Inspiring
July 10, 2006
Answered

Loading an Array of filenames from a text file

Hello all

I have a random picture flash animation that works with one frame only. The principle is that it loads files from an Array of filenames that is staticly filled out into an empty Clip object on the timeline. Works out great!

But the problem I have is that I have to make it dynamic. The customer must be able to add files as he wants through a system that works with text files (and I can't modify that system). So basically, he would upload the files and the system would add the new filename to a textfile from which the animation would add it to the array.

My question is how can I fill an array from a text file?

Thank you!
This topic is closed to new replies. Start a new post to keep the conversation going.
ThatnicknameCorrect answer
Inspiring
July 13, 2006
Ok nevermind, I found it!

Thanks to all for your help!

14 replies

kglad
Community Expert
Community Expert
July 13, 2006
you're welcome.
Translate
kglad
Community Expert
Community Expert
July 12, 2006
what code are you using?
Translate
ThatnicknameAuthorCorrect answer
Inspiring
July 13, 2006
Ok nevermind, I found it!

Thanks to all for your help!
Translate
Participating Frequently
July 11, 2006
Well first off your code is messign something


pathToPics="images/";
// fill this array with your pics
lv=new LoadVars();
lv.onLoad=function(){
pics=this.yourVar.split(",");


this line needs two commas remember from above?
pics=this.yourVar.split(",,");

as well as in your txt file
&yourVar=file1.jpg,,file2.png,,file3.swf,,etc&

between each file name is two commas.

thats the split
Translate
Inspiring
July 11, 2006
Ok I can't figure it out. Here's the code:

pathToPics="images/";
// fill this array with your pics
lv=new LoadVars();
lv.onLoad=function(){
pics=this.yourVar.split(",");
}
lv.load("images.txt");
this.fadeSpeed = 10;
this.pIndex = 0;
// MovieClip methods ----------------------------------
// d=direction; should 1 or -1 but can be any number
//loads an image automatically when you run animation
loadMovie(this.pathToPics+this.pics[random(11)], _root.photo);
MovieClip.prototype.changePhoto = function(d) {
// make sure pIndex falls within pics.length
this.pIndex = (this.pIndex+d)%this.pics.length;
if (this.pIndex<0) {
this.pIndex += this.pics.length;
}
this.onEnterFrame = fadeOut;
};
MovieClip.prototype.fadeOut = function() {
if (this.photo._alpha>this.fadeSpeed) {
this.photo._alpha -= this.fadeSpeed;
} else {
this.loadPhoto();
}
};
MovieClip.prototype.loadPhoto = function() {
// specify the movieclip to load images into
var p = _root.photo;
//------------------------------------------
p._alpha = 0;
p.loadMovie(this.pathToPics+this.pics[this.pIndex]);
this.onEnterFrame = loadMeter;
};
MovieClip.prototype.loadMeter = function() {
var i, l, t;
l = this.photo.getBytesLoaded();
t = this.photo.getBytesTotal();
if (t>0 && t == l) {
this.onEnterFrame = fadeIn;
}
};
MovieClip.prototype.fadeIn = function() {
if (this.photo._alpha<100-this.fadeSpeed) {
this.photo._alpha += this.fadeSpeed;
} else {
this.photo._alpha = 100;
this.onEnterFrame = null;
}
};

function changements(){
changePhoto(random(11));
}
setInterval(changements,4000);

Now using the debugger, I realize that it goes to the lv.onLoad function after he shows the first image. Why?
Translate
kglad
Community Expert
Community Expert
July 11, 2006
i disagree with andre, but that's ok. some people like xml files.

in your text file (say, yourTextFile.txt) you can use:

&yourVar=file1.jpg,,file2.png,,file3.swf,,etc&

in your swf use:

lv=new LoadVars();
lv.onLoad=function(){
yourArray=this.yourVar.split(",,");
}
lv.load("yourTextFile.txt");


//end of code:

(now, let's see an xml approach that's 100 times easier. or how about, just as easy.)
Translate
Inspiring
July 11, 2006
Great it works, but strangely enough the first picture is undefined...

Oh well, I'll try to figure that one out! Thanks you've been a huge help!
Translate
Inspiring
July 10, 2006
...create an XML file... it will be hundred time easyer for you to parse... (can output an array quite easily..)

and it's still easy to edit for the client...
Translate
Inspiring
July 10, 2006
Anyone?
Translate
Inspiring
July 10, 2006
Oh no I want the client to be able to add as much images he wants so he can modify the text file...

But I'm not sure how to use that solution (I'm quite new at AS you see) can you be more specific please? :)
Translate
kglad
Community Expert
Community Expert
July 10, 2006
list the files names separated by a delimiter. i like to use double comma (,,).

then after your text file has completed loading use the flash split(",,") method of your text file's variable to create your array.

if you don't want your client to edit your text file, set-up a php (or some other server-side) script to edit the file (and it may as well manage the upload).
Translate