Copy link to clipboard
Copied
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 );
}
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
Copy link to clipboard
Copied
You could put in a conditional that checks if " i " == 3 and when it does you add the event listener to the current box
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
"Good point, but I don't know if you explained myself well enough."
-That should have been "I" not "you",
"You're right, I did not explain yourself well enough, nor did you."
-LMAO.
Thanks for getting me on track.
Copy link to clipboard
Copied
You're welcome
Find more inspiration, events, and resources on the new Adobe Community
Explore Now