-
Event Listeners and efficiency
Hey all,
Lets say I had had a game where there were 100 enemies on the screen. The player can click on an enemy to interact with it.
Would it be better to have one event listener listening to the stage for mouse clicks, which would in turn call a function to see what they clicked on? (so 1 event listener in total)
Or to just have each enemy listening to see if they were clicked on (so 100 event listeners in total)
Sure, the first sounds better... but under the hood is it really the same thing?
Thanks
-
Senior Member
I would do the former. One listener for all. Less code is more efficient IMO, and since it's a game with 100 enemies each with its own listener, would cause major slowdown.
-
Senior Member
i add an event listener to the current movieclip that is being manipulated using e.target to so i would do something like make an event listner which is common to all moviclips such as MOUSE_OVER then assign another eventlistener with e.target.eventListener then when i'm through with that piece or movieclip i remove the event listener for that one.
-
Senior Member
I'd only do what steven suggests if all the moviclips are going to have the same actions.
-
Total Universe Mod
It's not really major slowdown to have a listener on each character nor is it extra code if they are all instances of the same parent class. All subclasses of eventDispatcher broadcast every event anyway, it's only the listener list lookup that costs extra.
Still, you are going to want to create a single listener that looks at event.target. Read up on event phases (capture, target and bubble) to understand why.
You'll be happy you did this should you ever switch to blitting your graphics for speed.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|