Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Best Practice for adding and removing eventlistners in AS3

Posted on 2009-04-27
1
Medium Priority
?
303 Views
Last Modified: 2013-11-11
Dear Experts,

I am trying to establish the most efficient way [least memory use] of adding eventlisteners.
Two examples below  show the "usual" method and secondly  something that I am working on which remove unnecessary eventlisteners during the process.

Can anyone tell me if there is any benefit in using the second method?
Thanks

 
---------------------------------"Usual" Method ----------------------------------------------
 
Button.addEventListener(MouseEvent.MOUSE_OVER, onButtonMouseOver);
Button.addEventListener(MouseEvent.MOUSE_OUT, onButtonMouseOut);	
}
 
		
public function onButtonMouseOver(event:MouseEvent):void{
                trace ("Mouse Over");		
		}
		
		public function onButtonMouseOut(event:MouseEvent):void{
			trace ("Mouse Out");		
		}
 
 
 
 
 
 
 
--------------------------- Second Method------------------------------------------------------
 
 
Button.addEventListener(MouseEvent.MOUSE_OVER, onButtonMouseOver);
}
 
		
public function onButtonMouseOver(event:MouseEvent):void{
                trace ("Mouse Over");
		Button.removeEventListener(MouseEvent.MOUSE_OVER, onButtonMouseOver);
		Button.addEventListener(MouseEvent.MOUSE_OUT, onButtonMouseOut);		
			
		}
		
		public function onButtonMouseOut(event:MouseEvent):void{
			trace ("Mouse Out");
			Button.removeEventListener(MouseEvent.MOUSE_OUT, onButtonMouseOut);
			Button.addEventListener(MouseEvent.MOUSE_OVER, onButtonMouseOver);
				
			
		}

Open in new window

0
Comment
Question by:pixel1024
1 Comment
 
LVL 5

Accepted Solution

by:
-null- earned 1500 total points
ID: 24242351
Hi

Both ways are perfectly valid, but I would lean towards the first way, as the second way is creating overhead every time you hover in and out. Whilst in theory, the second method means that you only ever have one event being listened for, events really dont use that much memory, they are simply pointers to a function entry point when a certain message is read in the message loop.

Therefore, I would say that you would be better off with the first method.

-null-
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

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…
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 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.
Suggested Courses
Course of the Month11 days, 8 hours left to enroll

564 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