Solved

Creating Multiple Event Listeners using loops

Posted on 2009-04-08
10
340 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a very simple example to help those of you who are still migrating from AS2 to AS3 understand the redesigned event model in AS3. In AS2.0, event functions (that is, the function to be performed when an event is fired) were stored as a pro…
The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

717 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