Loop Button with MouseEvent
Currently I only manage to traced the last MC. How could I trace the correct MC properties?
private function levelsBG():void {
for (var i:Number=0; i<myXML.children().length(); i++) {
listText=new TextField ;
listMC=new MovieClip ;
listText.text=myXML.MEMBER[i].@NAME;
listMC.buttonMode=true;
listMC.mouseChildren=false;
listMC.addChild(listText);
addChild(listMC);
listMC.addEventListener(MouseEvent.MOUSE_OVER,listOver);
}
}
开发者_运维知识库 private function listOver(e:MouseEvent):void {
trace(e.target.parent.listText.text);
}
Well, it looks like you're doing something kind of screwy here.
It appears that, due to listText not being declared in the levelsBG function, it must be declared out at the class level, and you are overwriting a reference to that object in every iteration through your loop, so the only one that exists at the end, is the very last object created.
Then in your event handler, you are traversing up the display tree to the class in which that one reference lives, and tracing out the text of that one, so the appearance is that they are all the same.
If your intent is to trace the 'text' property of any given textfield that you've named listText, you will need to go about it a little differently. This snippet should work, but you might want to revisit your understanding of how class members work and can be addressed, as opposed to child DisplayObjects?
private function levelsBG():void {
for (var i:Number=0; i<myXML.children().length(); i++) {
listText=new TextField ;
listMC=new MovieClip ;
listText.text=myXML.MEMBER[i].@NAME;
listText.name = "listText";
listMC.buttonMode=true;
listMC.mouseChildren=false;
listMC.addChild(listText);
addChild(listMC);
listMC.addEventListener(MouseEvent.MOUSE_OVER,listOver);
}
}
private function listOver(e:MouseEvent):void {
trace(e.target.getChildByName("listText").text);
}
}
Since you are not changing their positions (x
& y
), each mc would appear on top of the previous one. Since all movieclips are of same size and the last one is on the top, only that mc will receive the mouseOver
event. Change their position in the loop using something like mc.x = i * WIDTH;
精彩评论