A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: howto loop through an object in as 3

  1. #1
    Senior Member fantasio's Avatar
    Join Date
    May 2000
    Location
    at the progress bar
    Posts
    409

    howto loop through an object in as 3

    hello,

    how do loop through an object in as3, to get its keys/properties?

    before i did:
    Code:
    for (var i in obj){
    trace(i)
    trace(obj[i])
    }
    what would be the equivalent in the modern times ?

    thanks and cheers

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You could a) try that code to see whether it works (it does)
    b) search the forums for "iterate over properties",
    c) read the livedocs for statements and operators: http://livedocs.adobe.com/flash/9.0/...tatements.html

  3. #3
    Senior Member fantasio's Avatar
    Join Date
    May 2000
    Location
    at the progress bar
    Posts
    409
    ok, let me correct my question:

    i do not only want to loop through an object, but also through a MovieClips Props, the following code used to work and does not anymore:

    Code:
    var mc =new MovieClip()
    mc.graphics.lineStyle(0, 0x000000,0);
    mc.graphics.beginFill(0x990000);
    mc.graphics.drawRect(0,0,100,100);
    this.addChild(mc)
    
    for(var i in mc){
    	trace(i)
    }
    it seems, that you cant iterate through the properties of a Movieclip

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Exactly which properties were you expecting to see there? "graphics"?
    Seriously, try searching. You might find interesting threads.

    http://board.flashkit.com/board/showthread.php?t=776571

  5. #5
    Senior Member fantasio's Avatar
    Join Date
    May 2000
    Location
    at the progress bar
    Posts
    409
    Exactly which properties were you expecting to see there? "graphics"?
    dude, this was just an example.
    one method i used pretty often is to iterate to a movieclips properties and disable all textfields in this clip ... or many other possible uses.

    http://board.flashkit.com/board/showthread.php?t=776571
    does not help there either.

    anyways nevermind i will find an answer at some point.

    but i do not understand why you even try to help if you don't really want to.

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I do like to help. I don't like to guess at what the problem is. I have been a little snippy lately, and I'll try to tone that down.

    Properties are not the same thing as display children in AS3. It sounds like you're after the latter.

    If you're looking to iterate over the displayList children of a DisplayObjectContainer, you will probably want to use getChildAt.
    Code:
    for (var i:int =0; i < numChildren; i++){
      var d:DisplayObject = getChildAt(i);
      //do something with d, perhaps after testing to see if it's a textfield and casting to the correct type, etc.
    }

  7. #7
    Senior Member fantasio's Avatar
    Join Date
    May 2000
    Location
    at the progress bar
    Posts
    409
    dont sweat it, sometimes this job gets us impatient and snippy...
    regarding the problem:
    i think i still try to work with as3 the way i did with as1/2.
    this is not going to happen and i have to change my way of working instead of trying to do everything the old way.

  8. #8
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    AS3 for..in loops only go through dynamic properties (those you defined at runtime, not those that exist in the class's native definition).
    http://livedocs.adobe.com/flex/3/lan...s.html#for..in

    As the link above suggests, to get class definition members use describeType().
    http://livedocs.adobe.com/flex/3/lan...describeType()

    Display object instances created in Flash Authoring will have their instance names defined in the class definition so they can be found in describeType, not for..in. If you just want to loop over child objects in a certain display object container (as 5TonsOfFlax suggested), you just need to loop through the display list using numChildren and getChildAt.
    http://livedocs.adobe.com/flex/3/lan...ml#numChildren
    http://livedocs.adobe.com/flex/3/lan...l#getChildAt()

  9. #9
    World Kit Vote Holder Abelius's Avatar
    Join Date
    Feb 2002
    Location
    US
    Posts
    963
    Actionscript Code:
    for (var i:uint = 0; i < this.numChildren; i++){
            trace (i+'.\t name:' + this.getChildAt(i).name + '\t type:' + typeof (this.getChildAt(i))+ '\t' + this.getChildAt(i));
    }
    Cordially,
    Abelius
    www.worldkit.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center