Skip to main content
Inspiring
February 23, 2011
Answered

how to access child object of a movieclip

  • February 23, 2011
  • 2 replies
  • 618 views

hi i made thumbnails dynamically and i waana access it's child movieclip

var totalThumbs:Array=new Array()

var ThumbMC:MovieClip()

var childThumb:MovieClip()

var xpos:Number=10

for(var i:Number=0;i<10;i++){

   ThumbMC=new thumbOb()  //thumbOb is movieclip class on library

    addChild(ThumbMC)

   ThumbMC.x=xpos

  childThumb=new animationclip() //animationclip is movieclip class on library

ThumbMC.addChild(childThumb)

ThumbMC.addEventListener(MouseEvent.Click,thumbClick)

   xpos+=100

}

function thumbClick(event:MouseEvent):void{

event.target.childThumb.gotoAndPlay("clicked") << THIS SHOWING ERROR

}

This topic has been closed for replies.
Correct answer Ned Murphy

There are a number of coding errors in what you show so I have to imagine you are not in strict mode as a minimum.  You should avoid using event.target and use event.currentTarget, otherwise you are likely to find the target is not what you expect it to be.  currentTarget always relates to the object that has the listener assigned, not children of that object (which using target can end up doing).

When you dynamically add content, the usual parent.child dot-syntax targeting doesn't work.  You have to start using other means of accessing the child.  One way is to store the child in some other reference (like an array) and target it directly.  Another way would be to use the getChildByName() method... Here's an example using that approach...

var xpos:Number=10;

for(var i:Number=0; i<10; i++){
   
     var ThumbMC=new thumbOb();  //thumbOb is movieclip class on library
    ThumbMC.num = i;
    addChild(ThumbMC)
    ThumbMC.x=xpos
   
    var childThumb=new animationclip() //animationclip is movieclip class on library
    childThumb.name = "childThumb"+String(i);
    ThumbMC.addChild(childThumb)
   
    ThumbMC.addEventListener(MouseEvent.CLICK,thumbClick)
    xpos+=100
}

function thumbClick(event:MouseEvent):void{
    var childThumb = MovieClip(event.currentTarget).getChildByName("childThumb"+String(event.currentTarget.num));
    childThumb.gotoAndPlay("clicked");
}

2 replies

Ned Murphy
Ned MurphyCorrect answer
Legend
February 23, 2011

There are a number of coding errors in what you show so I have to imagine you are not in strict mode as a minimum.  You should avoid using event.target and use event.currentTarget, otherwise you are likely to find the target is not what you expect it to be.  currentTarget always relates to the object that has the listener assigned, not children of that object (which using target can end up doing).

When you dynamically add content, the usual parent.child dot-syntax targeting doesn't work.  You have to start using other means of accessing the child.  One way is to store the child in some other reference (like an array) and target it directly.  Another way would be to use the getChildByName() method... Here's an example using that approach...

var xpos:Number=10;

for(var i:Number=0; i<10; i++){
   
     var ThumbMC=new thumbOb();  //thumbOb is movieclip class on library
    ThumbMC.num = i;
    addChild(ThumbMC)
    ThumbMC.x=xpos
   
    var childThumb=new animationclip() //animationclip is movieclip class on library
    childThumb.name = "childThumb"+String(i);
    ThumbMC.addChild(childThumb)
   
    ThumbMC.addEventListener(MouseEvent.CLICK,thumbClick)
    xpos+=100
}

function thumbClick(event:MouseEvent):void{
    var childThumb = MovieClip(event.currentTarget).getChildByName("childThumb"+String(event.currentTarget.num));
    childThumb.gotoAndPlay("clicked");
}

February 23, 2011

try:

MovieClip(event.currentTarget).childThumb.gotoAndPlay("clicked") ;

or

MovieClip(event.target).gotoAndPlay("clicked") ;

Inspiring
February 23, 2011

it is showing error

ReferenceError: Error #1069: Property thumbChild not found on code.thumb and there is no default value.