Hello there.
I would like to know why this piece of code doesn’t work.
When I try to dispatch a custom event “MyEvent” after receiving it from another dispatcher I get this error (at run-time):
TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::Event@a5c2101 in MyEvent.
at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at MyListener/::handleMyEvent()
at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at MyDispatcher/fire()
at MyListener/fire()
at testEvents$iinit()
I found a workaround as you can see below.
However I still do not understand why Flash cannot convert the event just received in MyEvent seen that the event just received is MyEvent!
Here it is the code.
[LEFT][FONT=Courier New][COLOR=#808080]// file: testEvents.as (the document class)[/COLOR][/FONT][/LEFT]
package{
import flash.display.MovieClip;
public class testEvents extends MovieClip{
public function testEvents():void{
// Creates MyListener that creates MyDispatcher
var mylistener:MyListener = new MyListener();
// Sets a listener for MyEvent
mylistener.addEventListener(MyEvent.MY_EVENT_1,handleMyEvent);
// Starts MyListener that starts MyDispatcher that sends MyEvent
mylistener.fire();
}
private function handleMyEvent(event:MyEvent):void{
trace(event);
}
}
}
[LEFT][FONT=Courier New][COLOR=#808080]// file: MyEvent.as[/COLOR][/FONT][/LEFT]
package{
import flash.events.Event;
public class MyEvent extends Event{
public static const MY_EVENT_1:String = "event1";
public function MyEvent(type):void{
super(type);
}
}
}
[LEFT][FONT=Courier New][COLOR=#808080]// file: MyListener.as[/COLOR][/FONT][/LEFT]
package{
import flash.display.Sprite;
public class MyListener extends Sprite{
var mydispatcher:MyDispatcher = null;
public function MyListener():void{
// Creates MyDispatcher
mydispatcher = new MyDispatcher();
// Sets a listener for MyEvent
mydispatcher.addEventListener(MyEvent.MY_EVENT_1,handleMyEvent)
}
public function fire():void{
// Starts MyDuspatcher that sends MyEvent
mydispatcher.fire();
}
private function handleMyEvent(event:MyEvent):void{
trace(event is MyEvent); // sure it is, in fact it returns true
// ERROR at Run-time: TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::Event@a5c2101 in MyEvent
dispatchEvent(event);
// To fix the Error I need to create another istance of MyEvent and dispatch it
// dispatchEvent(new MyEvent(MyEvent.MY_EVENT_1));
}
}
}
[FONT=Courier New][COLOR=#808080][/COLOR][/FONT][LEFT][FONT=Courier New][COLOR=#808080]// file: MyDispatcher.as[/COLOR][/FONT][/LEFT]
package{
import flash.display.Sprite;
public class MyDispatcher extends Sprite{
public function MyDispatcher():void{
// Does nothing
}
public function fire():void{
// Dispatch MyEvent
dispatchEvent(new MyEvent(MyEvent.MY_EVENT_1));
}
}
}
In attach you can find a zip that contains the files, just dezip it in a dir and open the FLA and launch it.
What am I missing?
Sorry is this is a lame question but although I think I’m quite skilled in AS1-prototype, I’m learning AS3 in these days so I feel like a newbie again