00:00
00:00
Newgrounds Background Image Theme

Sinnymon just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

for loops, setting xml data, length

601 Views | 15 Replies
New Topic Respond to this Topic

I've got 3 movieclips a_1, a_2, a_3

I've got this for loop:

var arr:Array = [a_1,a_2,a_3];
for (var sdt:int= 0; sdt < arr.length; sdt++) {
arr[sdt].pText.htmlText = myText[sdt]["name"];
// and a bunch of other text being set in the array
}

Problem is.... I don't want a_3 to = the 3rd item... I want it to be the last item on the xml. The xml's length.

I know how to do this, but just not with the loop.

Do I have to take it out the loop completely?
Means I've got to paste the whole for loops content for just the a_3 alone.

The XML's length can be referred to as: amountToShow

Response to for loops, setting xml data, length 2015-02-05 13:47:00


No, you just have to branch.

if arr[sdt] is ar_3, then use amountToShow to get the XML, otherwise, use sdt.

I bet you copy & pasted myText[sdt] on every line instead of creating a variable to hold it's value.
If so, create a variable.

Response to for loops, setting xml data, length 2015-02-05 16:49:32


At 2/5/15 01:47 PM, milchreis wrote: No, you just have to branch.

if arr[sdt] is ar_3, then use amountToShow to get the XML, otherwise, use sdt.

How can I set this up without applying all the lines twice?

if(array[sdt]==a_3){
arr[sdt].pText.htmlText = myText[amountToShow]["name"];
} else {
arr[sdt].pText.htmlText = myText[sdt]["name"];
}

Applied twice, kinda...

I bet you copy & pasted myText[sdt] on every line instead of creating a variable to hold it's value.
If so, create a variable.

Not entirely sure what you mean here... you mean it would be more beneficial to have myText[sdt]["name"] as text[0]?

Response to for loops, setting xml data, length 2015-02-05 17:11:02


As a rule of thumb (treat this is as a law): never use anything with [ ] brackets twice.
Instead of using "array[sdt]" twice (or more times) by copy and pasting, create a variable for that.

At 2/5/15 04:49 PM, Aprime wrote: if(array[sdt]==a_3){
arr[sdt].pText.htmlText = myText[amountToShow]["name"];
} else {
arr[sdt].pText.htmlText = myText[sdt]["name"];
}
var target:MovieClip = array[sdt];
var node:XML; //or XMLList, whatever applies to your situation
if (target == a_3)
{
    node = myText[amountToShow];
}
else
{
    node = myText[sdt];
}

target.pText.htmlText = node.name;

You should only need the brackets if you cannot use the dot operator to access the XML property or attribute. that's why I just used .name instead of ["name"]

Response to for loops, setting xml data, length 2015-02-05 17:13:23


Generally brackets are used if you're not sure a property exists or will throw an error.
Otherwise the dot operator should be sufficient.


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to for loops, setting xml data, length 2015-02-06 00:34:31


At 2/5/15 05:13 PM, egg82 wrote: Generally brackets are used if you're not sure a property exists or will throw an error.
Otherwise the dot operator should be sufficient.

square bracket has no added safety over the dot operator, if it crashes on one it crashes on the other. the square bracket benefit is that it's runtime vs compile time allowing dynamic access. But in this case, you are right foo["bar"] has should be foo.bar

That being said, I often use square bracket to communicate to other devs the dynamic nature of the container (native objects, movieclips), which you might be referring to.

Response to for loops, setting xml data, length 2015-02-06 02:47:07


You're right, kinda. If I'm using reflection I obviously don't want compiler errors saying methods, etc don't exist.
If I have classes that extend classes and I want to pass in the base classes but use the functions of the upper-level classes (or a mix of both classes) then reflection is the way to go.

However, calling a function added on an upper-level class through a base class (that you're not sure exists) will throw compiler errors all over the place. With the dot operator, anyway. With brackets, that's not the case.


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

At 2/6/15 02:47 AM, egg82 wrote: However, calling a function added on an upper-level class through a base class (that you're not sure exists) will throw compiler errors all over the place. With the dot operator, anyway. With brackets, that's not the case.

that's true if the variable is typed to base class, but sheesh, do an upcast for the sake of readability. if i saw: myPoint["thing_in_point_extender"] I might throw up.


At 2/6/15 02:53 AM, GeoKureli wrote: that's true if the variable is typed to base class, but sheesh, do an upcast for the sake of readability. if i saw: myPoint["thing_in_point_extender"] I might throw up.

There's other classes that could be made that can extend this base class that I'm not aware of, plus it's a waste to upcast when all you're looking for is a function.

Obviously the situation is a little unique, but even the "wasting mem and cycles" might be enough to deter some people.

Reflection is always hackish, it's mostly just "what's the least shit way to do this?"


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Hmm this is good to know. Still though... means I have to type it out twice. Which kinda sucks.

Thanks for teh help peoples

Edit: Especially milchreis

Response to for loops, setting xml data, length 2015-02-06 12:15:36


At 2/6/15 05:26 AM, Aprime wrote: Hmm this is good to know. Still though... means I have to type it out twice. Which kinda sucks.

No you don't.

As you can see, reading the name property happens outside any if/else block.
Just add the rest of your lines the way I did with the "name" line, using "target" as what should be modified and "node" as where the information should come from.

Know what I mean?

@Egg82:
Property names can include operators and other things that would not work with the dot operator, that's when the brackets are necessary. (to escape whatever there is in the property)

When rtfm one finds this example:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/operators.html#brackets_%28XML%29

trace(myXML["foo-bar"]);

That's the simple reason, no reflection or questionable dynamicness or whatever. ;)


At 2/6/15 12:15 PM, milchreis wrote: As you can see, reading the name property happens outside any if/else block.
Just add the rest of your lines the way I did with the "name" line, using "target" as what should be modified and "node" as where the information should come from.

Know what I mean?

Hmm yeah, I think I've got it now.
Do I not need to put it back in the for loop otherwise that if statement will only display once.

Also in regards to

var node:XML;

I can call node whatever I want. But how do I know exactly what to name "XML"?

Edit:

Error #1034: Type Coercion failed: cannot convert Object@2b575479 to XML.

Response to for loops, setting xml data, length 2015-02-06 13:30:04


At 2/6/15 12:50 PM, Aprime wrote: Also in regards to

var node:XML;

I can call node whatever I want. But how do I know exactly what to name "XML"?

Edit:

Error #1034: Type Coercion failed: cannot convert Object@2b575479 to XML.

It depends on what "myText" is. you mentioned xml in the title so I assumed it is an XML object.
So what is myText? Where do you define it?

Response to for loops, setting xml data, length 2015-02-09 04:58:45


At 2/6/15 01:30 PM, milchreis wrote: It depends on what "myText" is. you mentioned xml in the title so I assumed it is an XML object.
So what is myText? Where do you define it?

It's inside the XML.

<myText>
name|Aprime|website|Newgrounds
</myText>
<myText>
name|milchreis|website|Newgrounds
</myText>

Response to for loops, setting xml data, length 2015-02-12 12:44:09


At 2/9/15 04:58 AM, Aprime wrote:
At 2/6/15 01:30 PM, milchreis wrote: It depends on what "myText" is. you mentioned xml in the title so I assumed it is an XML object.
So what is myText? Where do you define it?
It's inside the XML.

<myText>
name|Aprime|website|Newgrounds
</myText>
<myText>
name|milchreis|website|Newgrounds
</myText>

Provide the real XML and not some derived example like this one. Or is this your real xml file? (it's not even valid)
Provide the code that loads and parses the XML file .

Response to for loops, setting xml data, length 2015-02-12 16:32:06


At 2/12/15 12:44 PM, milchreis wrote: Provide the code that loads and parses the XML file .

It's okay. I've fixed it now. Thank you :)