In my tower defense game and every tile has its own mouse event listener. I am wondering, would it be better to use one event listener (MouseEvent.CLICK) and then when it runs the function, should I just use evt.target to determine what was clicked, and then run an appropriate function?

I am programming the game for web and phone and I started to notice some lag on my phone so I am trying to optimize my code. Any other tips to speed up code would help too.

  • \$\begingroup\$ I don't have any experience with flash on mobile devices, but I find it hard to believe that it's connected to event-listeners. In what situations does your game lag? \$\endgroup\$ – bummzack May 24 '11 at 12:39
  • \$\begingroup\$ The enemies on the path just stop or move slow. I wanted to start optimizing the code. Mouse Events are one of the first things I thought of. Another thing that might be slowing down my game is the sorting of the enemies. When a wave appears, the enemies in the wave are put into an array. I have enemies that are faster than others so they will pass enemies on the path. I want my towers to shoot whichever enemy is in front. To do this, each time my game updates, I find the distance traveled and sort the enemies with a bubble sort depending on which one moved the furthest. \$\endgroup\$ – Sean May 24 '11 at 12:52
  • \$\begingroup\$ Hm tricky. If you can somehow manage to remove the event-listeners (by pre-populating the board, so that you won't need mouse events), that would probably be the easiest way to check if the events are actually the bottleneck here. \$\endgroup\$ – bummzack May 24 '11 at 13:05
  • \$\begingroup\$ I will try that. I was just wondering if one event listener for everything would still be better than creating an event listener for each individual object. \$\endgroup\$ – Sean May 24 '11 at 13:15
  • \$\begingroup\$ Sure, one event listener would also work. You could use the target property then. But not even that is required, since you could determine the clicked tile by calculating the index by using the mouse coordinates given by the event. That would allow you to use some light-weight components like Shape for your tiles, or draw the tiles directly to a bitmap. \$\endgroup\$ – bummzack May 24 '11 at 13:33

I doubt that the event listeners are the main bottleneck of your applications. However as you are trying to optimize your code, here are some useful informations to know about Events alternatives and performances.

The main alternatives to the Events are the Signals & Slots, Observers and indeed the direct Callbacks.

Performance tests and articles:

AS3 Signal libraries:

Finally, you can check this video tutorial about how to use as3signal

  • \$\begingroup\$ I have been looking at signals. I want to start using those to take some of the stress off my update function. \$\endgroup\$ – Sean May 24 '11 at 14:32
  • \$\begingroup\$ I think another thing that could help would be to use less movieclips and more sprites or just bitmap data. My problem is, I don't know how to add bitmaps to a sprite or to bitmap objects and then display them. \$\endgroup\$ – Sean May 24 '11 at 17:35
  • \$\begingroup\$ MovieClip inherit from the Sprite class. So a MovieClip is a Sprite with some special methods and properties mainly focused on the timeline concept. You should use MovieClips when you need to manipulate their 'timeline' (frames) else you'd better use the Sprites. (Beside this, you should consider to choose some 'accepted' answers on your previous questions. You didn't accepted any of them, and accepting the answers that helped you will make people more prone to answer new questions. Not this one however, it's still early and you can have other answers.) \$\endgroup\$ – Valkea May 24 '11 at 17:56
  • \$\begingroup\$ @Valkea, sorry about not selecting answers. I am still learning how everything on this site works. I selected the answers to my other questions. \$\endgroup\$ – Sean May 24 '11 at 18:09
  • \$\begingroup\$ No problem, it's just better if you want more answers ;). \$\endgroup\$ – Valkea May 24 '11 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.