English 中文(简体)
For Loop Only Returning Last Item in Array :(
原标题:

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.

最佳回答

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++;
}
问题回答

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.

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





相关问题
Disable button tooltip in AS3

I want to disable the tooltip on certain buttons. The tooltip manager seems to be an all or nothing solution. Is it possible to disable the tooltip for just one or two buttons?

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

AS3 try/catch out of memory

I m loading a few huge images on my flex/as3 app, but I can t manage to catch the error when the flash player runs out of memory. Here is the what I was thinking might work (I use ???? because i dont ...

Red5 Security Tutorial

I am looking for a step by step tutorial on securing Red5 from intrusion. This seems to be a question that comes up alot in a google search, but is never really answered in a way that makes sense to ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

visible property of DisplayObject

For example I have a hierarchy of movie clips. mc1 is a child of mc, and mc2 is a child of mc1. Turns out that when I set mc1.visible = false; mc2.visible stays true. Is that supposed to happen?...

热门标签