Search code examples
haxeflambe

What are the best practices for working with keyboard and mouse events in Flambe?


I have read the relevant post in the Flambe guide but couldn't see any use of the clone() function. The APIs of the various event classes (like the PointerEvent class) contain this note:

NOTE: For performance reasons, PointerEvent instances are reused by Flambe. Use clone() to retain a reference to an event.

I have 3 questions:

  1. Is it possible that a PointerEvent object will change even though my code is still working on it?
  2. When should I use the clone() function?
  3. Are there any other best practices that I should know about when dealing with events in Flambe?

Solution

    1. It should be the same in the same frame, but I'm not sure if this is certain. I use the rule, if you pass it to a different instance or want to reuse it later, clone it.
    2. When you want to store the event in next frame. I personally mostly copy the viewX/viewY values into a custom point (for example in case you want to detect a swipe yourself) so then there is no need for the full event to be cloned, but that my preference.
    3. Always dispose the events. They return a SignalConnection. Make this a private member and dispose it in the dispose function, or register it to a Disposer if you use it. This avoids memory leaks and unwanted behavior.