Creating Multiple Event Listeners using loops

Hi,

I have some objects that have similar functions on Mouse_Down, that is to make certain objects visible. A quick and dirty way to make this happen is to create one event listener for each object. However, i believe there must be an easy way to do this using For loop, given that the name of the objects are similar.

Below is the code i tried; however for one reason or another this code does not work.
The error output is :

1 Tracing i at beginning
2 Tracing i at beginning
3 Tracing i at beginning
4 Tracing i at beginning
5 Tracing i at beginning
6 Tracing i at beginning
7 Tracing i at beginning
8 Tracing i at beginning
9 Tracing i at beginning
10 Tracing i later
TypeError: Error #1010: A term is undefined and has no properties.
      at MethodInfo-199()
10 Tracing i later
TypeError: Error #1010: A term is undefined and has no properties.
      at MethodInfo-199()
10 Tracing i later
TypeError: Error #1010: A term is undefined and has no properties.
      at MethodInfo-199()
10 Tracing i later
TypeError: Error #1010: A term is undefined and has no properties.
      at MethodInfo-199()


I know i made a silly mistake here, but try as i might, changing things, i cant find it. First of all, i am not sure why the code below doesnt register 10 times.
The output : 10 Tracing i later <-- only happen once. And it seems that this["AComponent"+i] is a null object.

Any explanation on how to register multiple event listeners using loops?
And why does my code not work?

Thanks in advance.
for (var i:int=1; i<10; i++) {
	trace(i+" Tracing i at beginning");
	this["AComponent"+i+"Popper"].addEventListener(MouseEvent.MOUSE_OVER, 
		function(e:MouseEvent){
			trace(i+" Tracing i later");
			this["AComponent"+i].visible = false;
		}
	);
}

Open in new window

NeverEndingFlashStoriesAsked:
Who is Participating?
 
blue-genieConnect With a Mentor Commented:
ah that bit of information about the nesting matters :-) it's probably just a pathing issue.
0
 
blue-genieCommented:
this code assumes you have items

AComponent1Popper through AComponent10Popper on the stage.


what is  ----   this["AComponent"+i] ?
must also be on stage.

your loop won't loop 10 times but 9 times, if you want it to loop ten times your condition must be <=10
below code works as follows





for (var i:int=1; i<10; i++) {
      trace(i+" Tracing i at beginning");
      this["AComponent"+i+"Popper"].addEventListener(MouseEvent.MOUSE_OVER, thing);
            
}
function thing(e:MouseEvent){
                  trace(i+" Tracing i later");
                  //this["AComponent"+i].visible = false;
}

//output

1 Tracing i at beginning
2 Tracing i at beginning
3 Tracing i at beginning
4 Tracing i at beginning
5 Tracing i at beginning
6 Tracing i at beginning
7 Tracing i at beginning
8 Tracing i at beginning
9 Tracing i at beginning
10 Tracing i later
10 Tracing i later
10 Tracing i later
10 Tracing i later
10 Tracing i later
10 Tracing i later
10 Tracing i later
10 Tracing i later
10 Tracing i later

will always trace 10 as it's at the end of the loop.
0
 
blue-genieCommented:
ok replace the nameless function in the same syntax you had.
i.e. your code works, just make sure the items are on the stage and it will work.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
NeverEndingFlashStoriesAuthor Commented:
Hi blue,

thanks for the response! I re-read my question regarding the 10th loop, and i guess i was confused when i wrote it. Anyway, i know the code should work - because i have a similar one on a separate actionscript and it works fine. This one though is on a timeline, nested a couple layers within a symbol on the stage.

I am a little short on time, but hope to get you a sample of the file either later today or tomorrow...would be great if you would take a look at it. Thank you!


0
 
NeverEndingFlashStoriesAuthor Commented:
Hi blue,

I am so sorry for the late reply - work is always a little random; i am not always programming flash. Anyway,

this["AComponent"+i].visible = false;

I am not quite sure why it doesnt work, as you said, it seems to be a pathing issue, however it when i assigned the event listener. I attached the file, renamed it as txt. Thanks so much for the help!



Blue.txt
0
 
blue-genieCommented:
what exactly is the problem now?
i'm getting a compile error

TypeError: Error #1010: A term is undefined and has no properties.
      at MethodInfo-4()
0
 
NeverEndingFlashStoriesAuthor Commented:
No, err...a compile error? Not sure what they were called :)


Anyway, the error happens on mouse over , at the following line :

            this["Sticky1361Component"+"2"].visible = false;

Which means that Sticky1361Component2 is not being recognize as an object by AS...
           
What i am trying to do is simply make "Sticky1361Component2" symbol (which is on the stage) invisible.
           
The next obvious question is why dont i just write :

Sticky1361Component2.visible = false;  

Well thats because i would like to have on my screen Sticky1361Component1 to 100, and i would rather just make
Sticky1361ComponentX.visible = false, where X is 1 to 100.

So what i did was, i combined the two strings : "Sticky1361Component" + "2"  , used this[ ] to make it into an object.
Somehow, i am getting a term is undefined error. However if you do a trace of Sticky1361Component2 , it will be there.

Now this same code works when i did it on a different case, on a separate actionscript file. I think what i have done is an incorrect use of this , however, i am not sure what else to use.

I am sorry if i am about as clear as mud, blue.

0
 
NeverEndingFlashStoriesAuthor Commented:
Yay!

So, as you said before, its a pathing issue. The this refers to different objects when outside the eventListener and inside of it. As such, casting my string this["Sticky1361Component"+"2"].visible
produced an unidentified object.

What i had to do was save the object that this was originally referring to inside a temporary object called temporal, and cast the string into temporal. Below is the working code.


   
var temporal:Object = new Object;
for (var i:int=1; i<4; i++) {
    trace(this.name);
    temporal = this;
    this["Sticky1361Component"+i+"Popper"].addEventListener(MouseEvent.MOUSE_OVER,
        function(e:MouseEvent){
            temporal["Sticky1361Component2"].visible = false;
                                        }
    );
}


Cheers!
0
 
NeverEndingFlashStoriesAuthor Commented:
Thanks! Pointed me to the right direction.
0
 
blue-genieCommented:
i got the error when i tried to compile your file i.e publish. you'll see there's a Compiler Error Tab that should pop up error message.
odd that you don't get them.
anyways well done on getting sorted out.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.