Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

addEventlistener, array, as linkage

New Here ,
Jul 12, 2013 Jul 12, 2013

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 );

                              }

TOPICS
ActionScript
735
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jul 12, 2013 Jul 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

Translate
LEGEND ,
Jul 12, 2013 Jul 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 12, 2013 Jul 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 12, 2013 Jul 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 13, 2013 Jul 13, 2013

"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.


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 13, 2013 Jul 13, 2013
LATEST

You're welcome

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines