Groups | Blog | Home
all groups > flash actionscript > february 2004 >

flash actionscript : Loading array from external file


scourtaud
2/26/2004 9:53:39 PM
Hi,

I'm new to actionscript and experiencing a problem...

I made ? photo gallery that I would like dynamic(not finished) but for the
moment I have the following problem :

My gallery works when the picture name array is written in the movie :

myimg = ["photo1","photo2","photo3","photo4"];

I use this array to load the jpegs in the movie.
I am trying to load this array from a text file but it won't work.

I actualy tried the following code :

in flash : box.loadvariables("info.txt");

in txt file : myimg = ["photo1","photo2","photo3","photo4"];

I dosen't work....

Would someone be kind enough to correct my mistakes...

Thanks in advance.

Seb

Laiverd.COM
2/26/2004 11:05:58 PM
textfile:

&myimg=photo,photo2,photo3,photo4& // make sure there's no spaces around the
comma's

then in Flash use myFlashArray = myimg.split(",");

Manual has more info on the string.split method. Bottomline is that
variables from a textfile always arrive as a string in Flash. U use the
String.split(delimiter) function to create an array out of it. Compare:

myString="What a string this is";
myArray = myString.spli(" ");
trace(myArray);

will output:
What,a,string,this,is

And obviously you have to wait for the variables to arrive before performing
the split()

John

--
----------------------------------------------------------------------------
-----------
RESOURCES
http://groups.google.com/advanced_group_search?hl=en&as_ugroup=*flash
----------------------------------------------------------------------------
-----------
TUTORIALS at
www.laiverd.com
Flash & PHP Emailform
Using textfiles in Flash
----------------------------------------------------------------------------
-----------

scourtaud
2/27/2004 12:41:11 AM
Thank you for your help,

It Works but not completely...

It works great with a button :
on (release) {
.....
}
But I would like it to work with a clip :

onClipEvent (load) {
....
}
End it seems impossible...
The same code in the same clip gives "undefined" width the clip and the
correct string with the button...

Thank you again for your help...
Laiverd.COM
2/27/2004 11:39:12 AM
Maybe you should start 'studying' the loadVars Object which I feel is much
easier to use, especially as it comes with the onLoad event.

Try something like this

onClipEvent (load) {
// create a new loadVars object
myVars = new LoadVars();
// define what has to happen once the textfile has been loaded
myVars.onLoad = function(success) {
// if the file was successfully loaded
if (success) {
myFlashArray = this.myimg.split(",");
// just a trace to check; skip in final code
trace(myFlashArray);
// skip the else partprobably in final code; or replace with something
else
} else {
trace("file could not be loaded");
}
};
// load the textfile
myVars.load("info.txt");
}

Now remember that myFlashArray will belong to the timeline of the movieclip.
If you want to access it from _root, use

trace(clipInstancename.myFlashArray);

John

--
----------------------------------------------------------------------------
-----------
RESOURCES
http://groups.google.com/advanced_group_search?hl=en&as_ugroup=*flash
----------------------------------------------------------------------------
-----------
TUTORIALS at
www.laiverd.com
Flash & PHP Emailform
Using textfiles in Flash
----------------------------------------------------------------------------
-----------

AddThis Social Bookmark Button