Very weird Type Coercion Error
Hi to everyone!
I'm having an quite weird problem with Custom events. I'll explain now which problem I have.
I'm making a game and in one part I need to dispatch an custom created event which can be dispatched on each frame of the game. Basicaly, I have created ENTER_FRAME listener which calculates mouse position on the stage and depending on its position, it dispatches custom event with two parameters.
Now, because of reason that this custom event might be dispatched on each frame during some time (if user keeps a mouse on the same position within given rectangle) I wanted to instantiate that custom event only once and then just change one of two parameters of that custom event class so I can improve game's performance (I dont want to instantiate custom event every time I want to dispatch it). And now I get this kind of error:
TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::Event@51aba89 to events.MovingEvent.
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.display::Stage/dispatchEvent()
As you can see, my custom event class is MovingEvent class which extends Event class. I made some debug sessions and I saw that this error occurs on SECOND call of dispatchEvent function. When this custom event is dispatched for the very first time, everything is ok. I thought that maybe some of the variables aren't initiated but when I do trace of those two parameters and event class it self, parameters have values and event class exists in objects list.
The strangest thing is that when I instantiate custom event class everytime I want to dispatch it, I dont get this error at all. I've tried not to change anything in custom event, I just instantiated it inside main class and then tried to dispatch it more than once and of course, then I get this type of error. So I came to the fact that this error occurs only when I want to dispatch custom event for the second time without re-instantiating it even when custom event is already instantiated.
Do anyone has answer to this weird problem?
Thanks in advance!
P.S.: I will try below to short my code where I used this custom event.
MovingEvent class:
package events
{
import flash.display.Sprite;
import flash.events.Event;
public class MovingEvent extends Event
{
public static const MOVING:String = "AdventureMap_Move";
private var speed:Number;
public var baseLayer:Sprite;
public function MovingEvent(type:String, baseLayer:Sprite = null, speedVar:Number=0)
{
super(type);
this.speed = speedVar;
this.baseLayer = baseLayer;
}
public function set _speed(s:Number):void
{
this.speed = s;
}
public function get _speed():Number
{
return this.speed;
}
}
}
Code where it has been used:
private function init():void
{
if(!this.movingEvent)
this.movingEvent = new MovingEvent(MovingEvent.MOVING, AdventureMap.BASE_LAYER); //Here I have instantiated the custom event for the first time
this.addEventListener(Event.ENTER_FRAME, moveAdventureMap);
}
private function moveAdventureMap(event:Event):void
{
var mousePercent:Number;
var baseSpeed:Number;
if(this.mouseX > WIDTH*3/4)
{
mousePercent = (this.mouseX-WIDTH*3/4)/(WIDTH/4);
baseSpeed = -mousePercent*100;
this.movingEvent._speed = baseSpeed; //Here is the place where I want to change speed parameter of MovingEvent class before I dispatch it
stage.dispatchEvent(this.movingEvent); //Here is the place where error occurs on its second call
}
}