Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
    
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? –  bummzack May 24 '11 at 12:39
    
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. –  Sean May 24 '11 at 12:52
    
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. –  bummzack May 24 '11 at 13:05
    
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. –  Sean May 24 '11 at 13:15
    
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. –  bummzack May 24 '11 at 13:33

1 Answer 1

up vote 3 down vote accepted

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

share|improve this answer
    
I have been looking at signals. I want to start using those to take some of the stress off my update function. –  Sean May 24 '11 at 14:32
    
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. –  Sean May 24 '11 at 17:35
    
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.) –  Valkea May 24 '11 at 17:56
    
@Valkea, sorry about not selecting answers. I am still learning how everything on this site works. I selected the answers to my other questions. –  Sean May 24 '11 at 18:09
    
No problem, it's just better if you want more answers ;). –  Valkea May 24 '11 at 18:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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