PDA

View Full Version : AS3.0 -> Updating variables



kpxnamja
October 8th, 2008, 07:45 PM
Hello, I have reached an AS2.0 to AS3.0 migration problem.

My problem is finding out a way to allow multiple variables to be updated under a for loop with a variable number "i".
In the past, I was able to get by using this code:



for (i=0;i<totalText;i++){
["text"+i] = arrayB[i];
}
On the stage were dynamic textfields with variables text0 to text'totalText'
After the loop, everything in arrayB was allocated to the correct text variable on the stage.

However, I have had no luck in AS3.0
The close I have gotten was updating the right side.
The code I have at the moment is the following:



// Stage has totalText amount of dynamic texts that goes by the instance names: text0, text1...etc.

var holder:TextField();

for (var i:Number =0;i<totalText;i++){
holder = ["text"+i]
holder.text = arrayB[i];
}
The left always gives me an error.
Is there any work around this? Or is there a better way of achieving this task?

kpxnamja
October 10th, 2008, 07:43 PM
I've re-edited the first post for clarity. I also should note, that I'm aiming for in the simplest term, a means to dynamically load variables without actually hard coding each variable.

eg:
//instead of:
var1 = 0; var2 = 1; var3 = 2;
//optimal
for (code here)
["var"+i] = [Array of numbers]

Gar_Fonz
October 10th, 2008, 08:20 PM
it has been awhile since I've done something like this, but anyway, try something like
for(var i:int = 0; i< txtArray.length; i++){
holder = getChildByName("text"+i);
holder.text = String(arrayB[i]);// you could omit the string wrapper if the contents of arrayB are already strings

Gar_Fonz
October 10th, 2008, 08:30 PM
there's more than one way to do this, and here is some more code. Mainly you need to use the keyword this which I believe accesses the stage, and all of the display objects contained within it, because it is not placed within the scope of another function or object or mc, etc. try this:
for(var i:int = 0; i < 3; i++){
this["txt"+i].text = i; // where as txt0, txt1, and txt2 are all textfields with those instance names.
}

kpxnamja
October 10th, 2008, 08:52 PM
Hi Gar_Fonz!

I must be doing something wrong...
I seem to be getting a null reference point when using the above code.
while the lower code presents: A term is undefined and has no properties.

I tried enumerating all the variables as much as I could, but I've had no luck.
I've also tried to feed the code stubs, but the problem doesn't seem to resolve...

If all else fails here's my code.


myLoader.addEventListener(Event.COMPLETE, processXML); //Checks to see if XML has been loaded.

function processXML(e:Event):void {
myXML = new XML(e.target.data);
for(var i:int = 0; i < 3; i++){
this["txt"+i].text = myXML.DOC[i].@TYPE;
}
}

Krilnon
October 10th, 2008, 09:00 PM
this which I believe accesses the stage, and all of the display objects contained within it

It doesn't. The code that you posted before using getChildByName has a better chance of working, though you need to be sure that you are calling the method on the appropriate object. Items that you place on the stage in the Flash IDE are placed on the DisplayObject that you can access using the root property of one of its descendants.

If the code that you are writing is on the main timeline, then using getChildByName without specifying the implicit parameter will result in the method being called on the 'correct' object.

Gar_Fonz
October 10th, 2008, 09:02 PM
I got it to work for me, so that tells me that you might have something wrong with your xml file. That's normally what happens to me when I get an #1009 error and its around my xml.
here's the code that I used in the flash file
var request:URLRequest = new URLRequest("test.xml");
var myLoader:URLLoader = new URLLoader();
myLoader.addEventListener(Event.COMPLETE, processXML); //Checks to see if XML has been loaded.
var myXML:XML = new XML();
function processXML(e:Event):void {
myXML = new XML(e.target.data);
for(var i:int = 0; i < 3; i++){
this["txt"+i].text = myXML.DOC[i].@TYPE;
}
}
myLoader.load(request);

now you want to be sure that you have added the textfields to the stage, or created them with the necessary code.
Here's the xml file that I'm using:
<firstChild>
<DOC TYPE="CHILD 1"/>
<DOC TYPE="CHILD 2"/>
<DOC TYPE="CHILD 3"/>
</firstChild>

Krilnon
October 10th, 2008, 09:06 PM
Oh, regarding Gar_Fonz's use of this: It works in this case because the Flash IDE auto-generates properties of the MainTimeline class that correspond to all of the instances on the stage at compile-time.

Sorry if I was generating any confusion. :)

Gar_Fonz
October 10th, 2008, 09:09 PM
No, thank you for clearing that up Krilnon. I'm still learning the details of actionscript 3.0. I appreciate your knowledge. thank you

Gar_Fonz
October 10th, 2008, 09:13 PM
kpxnamja, are all of your textfield instances on the stage on the same frame as your code? if not, that could be generating your error

kpxnamja
October 11th, 2008, 01:34 AM
Oh wow... a MAJOR palm face on my part...
I tried Gar_Fonz's code several times and I finally realized my problem.
My text field instances didn't have the numbers themselves!!! The all were named txt.

Nevertheless, thank you so much for the help! I'm still trying to figure out AS3.0. Thanks again!
:D

Gar_Fonz
October 11th, 2008, 02:02 AM
sure, no problem, glad you figured it out.