Skip navigation
Currently Being Moderated

accessing elements in arrays: confusing

Aug 4, 2012 9:14 AM

Hi, accessing elements in an array is fundamental but I'm confused because I thought you had to use getChildAt(i) in a loop etc... to access them and "do something to them". However, I have some code that doesn't use getChildAt - it simply loops through the array. So why would I use getChildAt if I do't have to???

 
Replies
  • Currently Being Moderated
    Aug 4, 2012 9:29 AM   in reply to codeBeastAdobe

    A 'child' is a display object that is added to another display object. E.g. if I had a cat sprite and I added a hat onto it (using the addChild function) then the hat sprite would be a child of the cat sprite. It just so happens that all children of a display object are stored in an array.

     

    If you're looping through an array you'll just want to use a simple loop function

     

    e.g.

     

    for(var i:uint = 0; i < myArray.length; i++)

    {

         myArray[i].doSomething();

    }

     
    |
    Mark as:
  • kglad
    62,219 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 4, 2012 9:47 AM   in reply to codeBeastAdobe

    if your child object references are stored in an array, you can loop through the array.  if they are not, you can still loop through the children of the parent using getChildAt:'

     

     

    for(var i:int=0;i<dobjParent.numChildren;i++){

    // do whatever with dobjParent.getChildAt(i);

    }

     
    |
    Mark as:
  • kglad
    62,219 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 4, 2012 10:19 AM   in reply to codeBeastAdobe

    most of the things you stated are either incorrect or off-point.

     

    there's really very little similar between arrays and displayobjectcontainers except both can be used to loop through some displayobjects.

     
    |
    Mark as:
  • kglad
    62,219 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 4, 2012 11:16 AM   in reply to codeBeastAdobe

    you're welcome.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points