This is my code:
I am getting null value of a_str ( desired value is “Hello” ) , even when I am providing the context of the instance of Main class, while using Reflect.callMethod.
What context should I provide so that I get the desired value?
Main.hx
class Main extends Sprite
{
var a_str = "Hello";
var mc:MovieClip ;
public function new() {
super();
addEventListener(Event.ADDED_TO_STAGE, addedToStage);
mc = new MovieClip();
mc.graphics.beginFill();
mc.graphics.drawRect(0, 0, 100, 100);
mc.graphics.endFill();
mc.x = 100; mc.y = 200;
addChild(mc);
}
function addedToStage(e:Event):Void
{
removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
Misc.testCallMethod(this,mc);// , Reflect.field(this, "f"), []);
}
public function broadcaster(e:Event ):Void {
trace( 'Called');
trace(a_str); // This is showing null even when the context has been provided
}
}
Misc.hx
class Misc
{
public function new() {
}
public static function testCallMethod(pMain:Main,mc:MovieClip ):Void
{
var f2:Event->Void = Reflect.field(mc, "addEventListener");
var f:Event->Void = Reflect.field(pMain, "broadcaster" );
Reflect.callMethod(pMain ,f2, [ "click" , f]); //<<<context right now is pMain
}
}