Wednesday, April 7, 2010

AS3: "for..in" and "for each...in" statements


After trolling browsing the awesome Grant Skinner's Flash dev blog (gskinner.com/blog/) I came across an interesting bit of actionscript that up until now I was completely unaware of.

Let's say for example that you have set up an object containing the following data:


var myObject:Object = new Object();

myObject.name = "Ben";
myObject.age = 32;
myObject.married = true;


How would you retrieve this data in a manner other than referencing the objects children directly (ie "myObject.name") ... Here's how...

for each (var value:* in myObject) {

      trace(value);
}

The above use of the "for each...in" code pumps out the following in your tracer window:


Ben

True
32

On the other hand, say that I want to obtain the name of the object child that these values are associated with... simply remove the word "each" from the iteration statement and it reports on the actual child variable instead of the child variables value...


for (var pos:String in myObject) {

     trace(pos);
}


The resulting output to the tracer will be:

name

married
age 

You could even go one step further to retrieve the values of each variable in the myObject object as follows...



for (var pos:String in myObject) {
     trace(myObject[pos]);
}


The above will output:

Ben

True
32

Nice little twist on an iteration statement, eh?

3 comments:

  1. Sorry about the comment before man, my bad. I'm terrible with computers. Anyways, keep up the great work. Your games are among my top favorite games. Especially exmortis. I bought the third game and I thought it was amazing. I play it all the time. :)

    ReplyDelete
  2. Hey ben, im not the same anonymous as the other guy, but i wanted to ask you something. I want to post comments on your new digital designer blog, but it wont allow anonymous comments and I can't get a google account because of a problem with my computer or something. It always shuts down when i try to make a google account or any kind of account. Is there a way you can put anonymous comments onto the blog? If you can't it's ok, i wont be mad, but if you can, please do so, or atleast tell me why if you can't.

    ReplyDelete
  3. Hey Ben, I'm sorry if a lot of these other guys are giving you crap because exmortis 3 costs $20. Look man, your games are amazing. I bought exmortis 3 immediately when it came out. Hell, I would've bought it for $30 dollars! Your games are among my top favorites. The exmortis series is one of the best series i've ever seen. I'm really glad you made this game, and I bet a lot of other people are too! So anyways im sorry if a bunch of idiots are crying because the game costs a bit of money. You did remind them a long time ago, so they should've known already. Listen, I looked at that comment on your former exmortis 3 blog from "Gama" complaining about the money. People like him should be able to make money themselves. Basically, I'm just saying dont let these idiots slow you down man. You're games are great! Especially exmortis. Keep it up man!


    Big fan,
    Eli

    ReplyDelete