dcsimg
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Code problem: getting a child of a child using getChildAt()

  1. #1
    Senior Member
    Join Date
    Jan 2008
    Location
    UK
    Posts
    269

    Code problem: getting a child of a child using getChildAt()

    Hi Guys,

    Need a bit of help here! I'll explain what my set up is first:

    In my game, I have a number of level icons set up on the stage (movieclips), each with a padlock symbol on it (a child movieclip). All level icons are placed in a container movieclip call 'container'.

    Here's what I'm trying to do:

    hide the padlocks on selected level icons from within my code.

    To do this, I'm cycling through the children of the container movieclip to get at each level icon as follows:

    Code:
    for (var n=0; n<container.numChildren; n++) {
        var displayItem:DisplayObject = container.getChildAt(n);
        //etc.
    }
    That bit works nicely - I can get at each of the level icons which is now called 'displayItem'.

    But now, I want to get at the child of 'displayItem' (ie the padlock, so I can hide it. I was thinking:

    Code:
    var child:DisplayObject = displayItem.getchildAt(0);
    would do it, but I get a 'call to undefined method getChildAt through a reference with static type' error.

    This seems to be telling me that 'displayItem' doesn't have that particular method associated with it.

    With that attempt failed, I tried instead adding the padlocks to the level icons from within the code:

    Code:
    for (var n=0; n<container.numChildren; n++) {
        var displayItem:DisplayObject = container.getChildAt(n);
    
        var padlock:padlock_mc = new padlock_mc();
        container.displayItem.addChild(padlock);
    }
    .... but that gives the same error. There's something about the display item retrieved from getChildAt that won't let me get at it's methods/properties.

    I then tried converting the type from DisplayObejct to MovieClip:

    Code:
    container.(displayItem as MovieClip).addChild(padlock);
    but that threw a 'filter operator not supported' error.


    Any ideas how I should be going about this?

    Cheers.
    Last edited by _Ric_; 04-09-2008 at 11:19 AM.

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    This issue has been bouncing around the forums a lot lately...not all DisplayObject's have children (such as TextFields, or FLVs).

    Since you know your children are going to be MC's, just hard-cast the variable when you retrieve it:

    PHP Code:
    var displayItem:MovieClip MovieClip(container.getChildAt(n));
    var 
    padlock:MovieClip displayItem.getChildAt(n); 
    Alternately, you could just give your padlock an instance name and call it directly...or store references to your displayItems in an Array instead of looking them up...or create a Dictionary indexing each padlock by it's displayItem.

  3. #3
    Senior Member
    Join Date
    Jan 2008
    Location
    UK
    Posts
    269
    Ok - that was embarrasingly easy. Cheers!

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