I have a project that uses a single EventListener to intercept all Mouse clicks and react accordingly based on “event.target.name”. This works fine for Flash targets, but throws an error of “openfl.events.IEventDispatcher has no field name” on HTML5 and Windows targets. Is this behaviour expected, does “event.target.name” only work for Flash targets?
The code below works in Flash but not HTML5 or Windows
package;
import openfl.display.Sprite;
import openfl.Lib;
import openfl.events.MouseEvent;
class Main extends Sprite
{
var red = new Sprite();
public function new()
{
super();
red.graphics.beginFill(0xff0000);
red.graphics.drawRect(0, 0, 100, 100);
red.graphics.endFill();
red.name = "SpriteOne";
addChild(red);
stage.addEventListener(MouseEvent.CLICK, handleMouse);
}
function handleMouse( event:MouseEvent )
{
trace (event.target.name);
}
}
This is my first experience using any of the technologies associated with OpenFL, so I may be overlooking something fundamental.
Haxe getters/setters are a compile-time feature. if you do something like:
var target:Dynamic = mySprite;
target.x = 100;
… it won’t work correctly. It will try and change an x field directly, but at compile-time, Haxe replaces references to Sprites to the setter similar to:
target.set_x (100);
Because of this problem, letting e.target be Dynamic causes problems for many kinds of problems.
Use cast(e.target, URLLoader);, var urlLoader:URLLoader = cast e.target; or use a reference to your loader you may have saved elsewhere and it should work great
I’m still not really getting it. I understand I need to cast it and I did, but then I need to access the data property of the loader and that’s empty. What I am doing wrong?
private function onURLloaderComplete(e:Event){
var loader:URLLoader=cast(e.target, URLLoader);
trace("result:"+loader+" data:"+loader.data);