Skip to main content
Known Participant
July 12, 2013
Answered

addEventlistener, array, as linkage

  • July 12, 2013
  • 1 reply
  • 796 views

I have added 10 movieclips with the AS linkage "Box" in this function. I've named the different instances "layer1" to "layer10"

My question is, how can one add an eventlistener to, lets say, only "layer4"?

var NUM_BOXES:int = 10;

var BOX_SPACING:int = 1;

 

var _boxes:Array = [];

 

function Test()

                    {

                              for (var i:int = 0; i < NUM_BOXES; i++)

                              {

                                        var box:Box = new Box( i + 1 );

                                        box.y = (box.height + BOX_SPACING) * i;

                                        box.name= "layer" +( i + 1);

                                        box.buttonMode = true;

                                        box.addEventListener( MouseEvent.MOUSE_DOWN, onBoxPress );

                                        box.addEventListener( MouseEvent.MOUSE_UP, onBoxRelease );

 

 

                                        addChild( box );

                                        _boxes.push( box );

                              }

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

You're right, I did not explain yourself well enough, nor did you.

The name property of an object is not the same as the instance name, so you cannot target it directly using the name property, but you can indirectly target it in the manner of...

getChildByName("layer4").addEventListener....etc

1 reply

Ned Murphy
Legend
July 12, 2013

You could put in a conditional that checks if  " i " == 3 and when it does you add the event listener to the current box

Rhov23Author
Known Participant
July 12, 2013

Good point, but I don't know if you explained myself well enough.

Isn't there a way to use the "box.name" directly in the addEventlistener?

Ned Murphy
Ned MurphyCorrect answer
Legend
July 12, 2013

You're right, I did not explain yourself well enough, nor did you.

The name property of an object is not the same as the instance name, so you cannot target it directly using the name property, but you can indirectly target it in the manner of...

getChildByName("layer4").addEventListener....etc