Rihards
Rihards

Reputation: 10349

Efficient event listening in ActionScript 3

I have a map of items and I need to know when a mouse moves over the item. Should I add a event listener for mouse over and mouse out to each item (there may be a lot) or I should add mouse over and mouse out listeners to whole container and do some checking to detect whether the target has item on it or not?

In the second way it would mean that the event would occur on entering each map tile in the container i would be listening. This seems a bit pointless, but I heard somewhere that I should add as little as possible.. So what should I do?

Upvotes: 0

Views: 316

Answers (2)

Muhammad Irfan
Muhammad Irfan

Reputation: 1457

Well its matter of preference. You will not get performance issue in any case. However, in the Event.ENTER_FRAME Listener, you should be very careful writing you scripts as this is executed per frame.

I think you should add Mouse-Listner to item as mentioned above. As listener are attached to an object/item, will be removed when item or object is deleted by garbage Collector.

Upvotes: 1

Emil
Emil

Reputation: 113

If the items is just going to do the same thing, then I would probably suggest with the first and create a simple class that you assign to the object.

package {
  import flash.display.MovieClip;
  import flash.events.MouseEvent;
  public class Item extends MovieClip {
    public function Item() {
      this.addEventLister(MouseEvent.OVER, onMouseOver);
      this.addEventLister(MouseEvent.OUT, onMouseOut);
    }
    private function onMouseOver(e:MouseEvent):Void { trace("mouse over"); }
    private function onMouseOut(e:MouseEvent):Void {trace("mouse out");}
  }
}

Upvotes: 2

Related Questions