Solved

Creating Multiple Event Listeners using loops

Posted on 2009-04-08
10
334 Views
Last Modified: 2013-11-11
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

0
Comment
  • 5
  • 5
10 Comments
 
LVL 39

Expert Comment

by:blue-genie
ID: 24149048
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
 
LVL 39

Expert Comment

by:blue-genie
ID: 24149066
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
 

Author Comment

by:NeverEndingFlashStories
ID: 24161969
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
 
LVL 39

Accepted Solution

by:
blue-genie earned 500 total points
ID: 24166815
ah that bit of information about the nesting matters :-) it's probably just a pathing issue.
0
 

Author Comment

by:NeverEndingFlashStories
ID: 24189100
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 39

Expert Comment

by:blue-genie
ID: 24203046
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
 

Author Comment

by:NeverEndingFlashStories
ID: 24205194
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
 

Author Comment

by:NeverEndingFlashStories
ID: 24206151
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
 

Author Closing Comment

by:NeverEndingFlashStories
ID: 31568317
Thanks! Pointed me to the right direction.
0
 
LVL 39

Expert Comment

by:blue-genie
ID: 24212274
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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How do I convert a CS4 FLA file to CS6? 1 847
File drag and drop 6 127
Adobe Flash Player ActionScript error 6 50
Adobe Indesign CS6 4 76
Recently, I was asked to recommend a tracking system to be implemented on a clients website. As the entire site was built on flash, my first thought was to suggest custom built tracking system. However, our company at that point of time didn't h…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now