Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable scope/array problem in Flash 5

Status
Not open for further replies.

kdelacruz

Technical User
Oct 7, 2002
25
ES
Hello --
I am new to Flash but have some VB experience. I am trying to make a simple (or so I thought!) movie that's basically a text box displaying a list of current events, one by one. I want to store the events in an array, and then have the first one display for a few seconds, then go to the next array item, then the next, and so on.
I have a movie, with 2 layers: Actions and Display. My code so far is in the first frame of the actions layer.
The Display layer contains a dynamic text box set to the variable "newsitem" - this is where I want to display my events.

I have a text file (news.txt) containing the events, each event separated by a carat ("^").
Contents of text file:
txtmsg=October 20-21, 2002 FIN User Conference, Van Nuys, CA^November 2-3, 2002 Vancouver Tech Conference, Vancouver, BC^December 4-5, 2002 Another Trade Show, New York, NY

I load the text file contents into a Flash variable (txtmsg) (this part works fine). I then use the split function to separate out each event into its own array item. It works fine (meaning when I test the movie, and choose Debug - List Variables, each event is listed as an array item.)
BUT, if I add more frames to the display layer, and test the movie again, the array is undefined. For some reason the variable txtmsg still contains all the events.
Here's my code from frame 1 of the actions layer:
loadVariablesNum ("news.txt", 0, "POST");
mycounter = 0;
newsarray = txtmsg.split('^');
newsitem=newsarray[mycounter];

My plan was to add maybe 20 frames, where the first newsarray item would display, then add another piece of actionscript in frame 20 something like this:
mycounter++;
newsitem=newsarray[mycounter];
gotoAndPlay (2);

then just have it loop

I don't know if by adding more frames, somehow the variable containing the array is going out of scope, or what!
Can someone please help?

Thanks!
Kathy
 
This is probably what's happening - you're trying to access the array before the variable has loaded in the data from the text file. This will give you an 'undefined' result.

The reason it worked before you added the frames is that the movie was looping on one frame only - the array would be filled after a few iterations of that loop. Once you added the frames the movie runs on immediately from the first frame - the array values aren't constantly being updated and therefore the array is showing empty...

To get around this build in a delay right at the start of your movie to allow Flash to load the variables before moving on (this is especially important once the movie is online as the loading process over HTTP is obviously longer). The best ways to do this are either to build in a loop with a gotoAndPlay() statement which doesn't break out of the loop until the last array index is filled, maybe:

while(newsArray[n]=='undefined'){
gotoAndPlay(2);
}

...or if you're confident with clipEvents then you can use onClipEvent(data) to trigger a script.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top