views:

241

answers:

4

I'm an actionscript novice so I hope I'm missing something simple here. In a nutshell, I have a for loop updating a dynamic text element but it seems to iterate so fast that only the last item in the loop is updating the text. I'd like the dynamic text element to be updated with EACH item in the loop, not just the last.

I have a single frame movie where I create a simple array of strings from an external text file:

_global.i = 0;
_global.numplaces = 0;
_global.PlacesArray = new Array();

loadedText = new LoadVars();
loadedText.onLoad = function() {
    filecontents = this.places;
    _global.PlacesArray = filecontents.split(";"); //parse the file string
    _global.numplaces = _global.PlacesArray.length-1;
    };
loadedText.load("listofplaces.txt");

In a nested movie ("places"), I have a for loop:

for (_global.i=0; _global.i<_global.numplaces; _global.i++) {
    trace("global i is "+_global.i);
    trace(_global.PlacesArray[_global.i]);
    CityState.place.htmlText = _global.PlacesArray[_global.i];
}

The traces work fine and show that the for loop is iterating properly. However the place.htmlText dynamic text in a movie nested one level down (for tweening purposes) that I'd like the loop to update is only updating with the last item in the array.

+6  A: 

Hi there, I'm not an actionscript coder, but based on the for loop above it looks like you are always assigning to the htmlText property, instead of appending to it:

CityState.place.htmlText = _global.PlacesArray[_global.i];

I'm not sure if as2 supports a += operator, but appending the values might fix this.

Jay S
Exactly my thoughts, but I also don't know ActionScript.
Michael Myers
A: 

I don't know what this is:

CityState.place.htmlText = _global.PlacesArray[_global.i];

But it most likely is your problem if what you want is to simply append the text. It looks (deduced from coding convention) like a static variable called place, a dynamic text field, in a class called CityState. If this is correct, then your loop is doing exactly what it's told to do, loop over all items, set the text of each item to the text field. When the loop is done, the text field will have the value of the last item your referencing in the loop.

If what you want is to append text, try this (note the plus sign after the equals sign):

CityState.place.htmlText += _global.PlacesArray[_global.i];

But to be honest, I can't really tell from your post if you want to append text or create a new text field for each item in PlacesArray.

macke
Thanks for responding...I don't want to append text. I want the text field to show, with the fade-in, fade-out animation:_global.PlacesArray[_global.1]then (fade out)_global.PlacesArray[_global.2]then (fade out)_global.PlacesArray[_global.3]etc.I guess maybe I need to put the loop at a higher level?? I don't write code so apologies if I'm not as quick with the logic.
Colbay
You said: "If this is correct, then your loop is doing exactly what it's told to do, loop over all items, set the text of each item to the text field. When the loop is done, the text field will have the value of the last item your referencing in the loop."But I'm telling the movie to set the text of the textfield WITHIN the for-loop. Shouldn't I see this change EVERY time the loop is iterated? Why does it seem like it's looping through all items in the array FIRST, THEN displaying the textfield text change?
Colbay
Code running in Flash execute in alotted time frames (ie, frames). So basicly, your loop is executing in one frame, with the effect of only the last text displaying. If you want to change the text when your tween is completed or some such, you'd have to hook up some sort of event handler or callback to your tween library and set the text that way. Judging by your own answer, I assume your code above is executed in such a handler or equivalent. It would be helpful if you were more clear with your intentions and also added all relevant code. Good that you solved it though!
macke
A: 

Got help from elsewhere... I just changed #2 to: _global.i++ if (_global.i>=_global.numplaces) { _global.i=0; } CityState.place.htmlText = _global.PlacesArray[_global.i];

to get what I want :)

Now I just need to figure out how to make the parent movie wait until the child movie is loaded before updating the text (the first item is shown undefined right now because the child movie runs before some global variables are not initialized).

Colbay
is the original question answered then? please mark the correct answer and close the question. :-)
BerggreenDK
A: 

At top level of the movie, load the external file ONLY ONCE and parse into an array:

loadedText = new LoadVars();
loadedText.onLoad = function() {
    filecontents = this.places;
    _global.PlacesArray = filecontents.split(";"); //parse the file string
    _global.numplaces = _global.PlacesArray.length-1;
    _global.i = 0;
    CityState.place.htmlText = _global.PlacesArray[_global.i];
};
loadedText.load("listofplaces.txt");

At the first frame of the child movie (2 levels down), feed the contents of the array one at a time to a dynamic textfield:

if ( _global.PlacesArray ) {
    if (_global.i>=_global.numplaces) { 
       _global.i=0; 
    };

    CityState.place.htmlText = _global.PlacesArray[_global.i];
    _global.i++;
}
Colbay