A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Event Listeners and efficiency

  1. #1
    Game Developer
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    292

    Exclamation 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

  2. #2
    Senior Member Steven FN's Avatar
    Join Date
    Mar 2010
    Location
    CA, USA
    Posts
    276
    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.

  3. #3
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    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.
    ~calmchess~

  4. #4
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    I'd only do what steven suggests if all the moviclips are going to have the same actions.
    ~calmchess~

  5. #5
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center