Does "event.target.name" only work for Flash targets?

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.

Many thanks in advance.

You should use casting:

var myButton:Sprite = cast(event.target,Sprite);
trace(myButton.name);

Thank you so much for your help, that did the trick.

event.target used to be Dynamic, but this broke getter/setter support, and hurt performance on static platforms. Sorry about the inconvenience :slight_smile:

So I used to be able to do something like this:

private function onURLloaderComplete(e:Event){
		trace("onURLloaderComplete result:"+e.target.data);

to what do I have to cast the target now in order to access .data?

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 :slight_smile:

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);

result:[object URLLoader] data:

Is it a URLLoader, or Loader (with contentLoaderInfo)

Is it the string format type, or ByteArray?

Perhaps check if the response is null, and if not, what length it is

var byteArray:ByteArray = loader.data;

might also be helpful, to be sure the dynamic data is cast correctly (or similarly with a String type)