I have this array:
move = new Array("c1+1", "p2=4","c1=3", "p4=3","cT+1","p8=2","c5+1");
How can I load this from a txt file?
I use loadVariables and it doesn't load anything.
Printable View
I have this array:
move = new Array("c1+1", "p2=4","c1=3", "p4=3","cT+1","p8=2","c5+1");
How can I load this from a txt file?
I use loadVariables and it doesn't load anything.
The text file content must conform to MIME format.
Replace your "=" with "%3D"
Replace your "+" with "%2D"
Thus:
Move=c1%2B1, p2%3D4,c1%3D3, p4%3D3,cT%2B1,p8%3D2,c5%2B1
This will load Move as a string;
Then Move=Move.split(","); It will split the string into an array by ",";
It still doesn't work.
This is what I have in the txt file:
&move=c1%2B1, p2%3D4,c1%3D3, p4%3D3,cT%2B1,p8%3D2,c5%2B1
and this is what I code in my movie
loadVariables ("move.txt", 0);
move = move.split(",");
Can you guys help me out here. It seems the loadVarialbes does not work.
By the way, what would be the code for - sign in MINE format?
Thanks
For "-", it is %2D; It is nothing special, just a binary number. For space " ", it is %20. Familar right ? Just check the ASCII table.
Well, you can not loadVariables and pick that value immediately. Even you put p=3 in text file, you can not access variable p immediately.
You must use onClipEvent(data) or other script to wait for variable loaded.
Make a dynamic text with variable name "Move"; After varaibles loaded, the text shows automatically. Then you will see what you got.
Even I wait for 5 mins, it still doesn't load it. Argggg, this really drive me nut. Please help
Add an empty movieClip to stage; instance name "MC";
onClipEvent(load){loadVariables("move.txt",this);}
onClipEvent(data){_root.Move=Move.split(",");}
Now you can access _root.Move[2] etc;
5 min ?
please check move.txt. Try add & at the begining and end;
&Move=c1%2B1, p2%3D4,c1%3D3, p4%3D3,cT%2B1,p8%3D2,c5%2B1&
You can make a button to trace. on(release){trace(Move[2]);}
The _root.MC.Move will be a string;
The _root.Move will be an array;
You can also have;
&move=c1%2B1, p2%3D4,c1%3D3, p4%3D3,cT%2B1,p8%3D2,c5%2B1&done=true
then check if(done==true) before splitting the string.
~mgb
Thanks Ericlin, I try your code and it works. But now I have a question. Before I try you code, I change loadVariables to loadVariablesNum, and it loads the variables instantly. I just use
loadVariablesNum ("move.txt", 0);
on the first frame of the main movie instead of
loadVariables ("move.txt", 0);
Can you explain why?
Thanks
As I know, they are the same. "loadVariableNum" is not well documented. As macromedia said, it appears because of some one script like this: loadVariable("move.txt", job); They do not know the variable "job" is a level number or movieClip name. So, loadVariablesNum("move.txt", job) is clearer. Nothing special.
Your previous script use loadVariable("move.txt",0) in a MovieClip. The variable is load to _root, not in the movieClip. The next line: move=move.split(","); is with wrong taget path;
You moved the script to frame script. Still, you should not access it "instantly". Now onClipEvent(data) can not be trigered any more. You must follow the technique suggested by the post of mgb.
Ericlin, thank you so much.
Now I got everything works, but I just want to make my app simpler, without using onClipEvent(load)and
onClipEvent(data) attached to an empty mc. So I use mgb technic.
This is my movie look like. My movie has only 1 frame, and I attach this script to that only frame.
loadVariablesNum("move.txt", 0);
if (loading eq "true"){
move = move.split(",");
}
the move.txt look like this:
&move=c1%2B1,p2%3D4,c1%3D3,p4%3D3,cT%2B1,p8%3D2,c5 %2B1&loading=true
When I test this movie, the output look like this:
Level #0:
Variable _level0.$version = "WIN 5,0,30,0"
Variable _level0.move = "c1+1,p2=4,c1=3,p4=3,cT+1,p8=2,c5+1"
Variable _level0.loading = "true"
My question is, how come it doesn't split that string, and creat an array? What is my error? It seems like it never get inside that IF clause even though the condition is true
Thank you for all your patience, and hope you dont mind me asking too much question.
Well, frame 1 is executed only once. The first line, it starts loadingVariables. When Flash meets the second line, move is still mothing. So, it would not split.
Both onClipEvent(data) and loading=="true" technique are not so simple as we post. You need a waiting loop.
For example. Frame 1 loadVariables. Frame 2 and Frame 3 is a loop, when loading="true", split the variable and jump to Frame 4 to start your main movie.
Becareful do not place "loadVariables" in a loop.
The second thing:
Even you scceed in this. In your output window, array is always shown as array.toString. You will got the same output as before.
Thank you Ericlin. I have learned so much from you guys.
Oh, one more question, are you chinese?
Hey guys.
I've used a similar method to get array data from an ASP page.
I use the array.split("~") to split my long string into an array, however this seems to be extremely slow.
I need to load about 2-4k of data into a few arrays, and it seems to take such a long time just doing the first split that I get a "script taking too long" error message.
Something like this in ASP is extremely fast, so why is it so slow working with arrays in Flash?
Is there any other way to do this?
Is there a way to extend the timeout period so people dont get the error message?
Thanks.
check your code, maybe a bug somewhere. my string is very long too, (it's actual a string contains all the move from chess games) and its size is 5K. Or maybe that come from ASP not from text file?