开发者

For Loop Only Returning Last Item in Array :(

开发者 https://www.devze.com 2022-12-12 07:00 出处:网络
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 th

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开发者_JAVA技巧 = 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.


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.


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.


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++;
}


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消