This could be a Haxe issue, an OpenFl issue, or a user error. I’m not smart enough to know and since my example entangles OpenFl EventDispatcher I’ll start here. Please let me know if I should post in Haxe’s bug reports on Github.
I use Reflect.field() to reference a function from a string. The function calls works fine when targeting Windows \ Debug. But when targeting HTML5 (or HTML5 \ Debug), functions within the function called via Reflect.field() are not found.
Haxe 4/0/2
OpenFl 9.0.2
Main.hx
package;
import openfl.display.Sprite;
import openfl.events.Event;
class Main extends Sprite
{
private var testDispatch :TestDispatch;
public function new()
{
super();
testDispatch = new TestDispatch();
addChild(testDispatch);
testDispatch.addEventListener("TEST_DISPATCH", onHeardEvent);
}
private function onHeardEvent(event :Event) :Void
{
trace("I heard the event!");
}
}
TestDispatch.hx
package;
import openfl.display.Sprite;
import openfl.events.Event;
import openfl.events.MouseEvent;
class TestDispatch extends Sprite
{
private var _eventToDispatch :Event;
private var _rndRect :Sprite;
public function new()
{
var funcName :String;
super();
_eventToDispatch = new Event("TEST_DISPATCH", true, false);
_rndRect = new Sprite();
_rndRect.graphics.beginFill(0x404040, 1);
_rndRect.graphics.drawRoundRect(20, 20, 50, 50, 10, 10);
addChild(_rndRect);
_rndRect.addEventListener(MouseEvent.MOUSE_DOWN, dispatchIt, false, 0, true);
funcName = "dispatchIt";
_rndRect.addEventListener(MouseEvent.MOUSE_UP, Reflect.field(this, funcName), false, 0, true);
}
public function dispatchIt(event :MouseEvent) :Void
{
trace("dispatchIt() event.type = " + event.type.toString());
dispatchEvent(_eventToDispatch);
}
}
The MOUSE_DOWN listener directly uses the event handler function name.
The MOUSE_UP listener use Reflect.field() with a string containing the name of the event handler function.
MOUSE_DOWN works, MOUSE_UP doesn’t, as you can see from the trace() messages in the screen grab below. Again, this works fine when targeting Windows
I have the same problem trying to call functions within the class as I have with trying to call dispatchEvent();
In my real-world program, I can come up with something other than the Reflect.field() call to get around this.
I suppose I should try stripping out OpenFl and seeing I can reproduce the problem with just Haxe, but I’ve spent days just figuring out that Reflect.field() was causing my problems and I have to get moving on my project for now.
Any thoughts?
Thanks.