Skip to main content
Known Participant
November 6, 2012
Answered

Very weird Type Coercion Error

  • November 6, 2012
  • 1 reply
  • 4354 views

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

           }

}         

This topic has been closed for replies.
Correct answer kglad

As I said before, this happens on and after second event dispatching without reinstantiating it at every dispatch. If I reinstantiate it as MovingEvent, no error occurs at all, but I think its a bit stupid to instantiate class every time I want to use it if I just want to change one parameter of it.


that's a flash bug.

to work-around, recreate the event each time you need to dispatch it but do not create a new variable for the event (to prevent a memory leak):

movingEvent = new MovingEvent(MovingEvent.MOVING, AdventureMap.BASE_LAYER);

stage.dispatchEvent(this.movingEvent);

1 reply

kglad
Community Expert
Community Expert
November 6, 2012

what's trace(this,this.movingEvent) show?

Known Participant
November 6, 2012

Hi kglad and thanks for your reply!

When I put trace you asked just before dispatchEvent function I get two times this output:

[object Canvas] [Event type="AdventureMap_Move" bubbles=false cancelable=false eventPhase=2]

and then immidiatelly after it I get error from above which points on dispatchEvent function. This is exactly what I wrote above. First time it goes very well and second time it crashes for unknown reason for me. It's like that some type conversion happens on first dispatchEvent function call. By the way, Canvas is a class which contains these two functions written above.

kglad
Community Expert
Community Expert
November 6, 2012

what's the relationship between MovieEvent and AdventureMap_Move?

and copy and paste the output from the following including your complete error message after clicking file/publish settings/swf/tick "permit debugging".

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

trace("a",stage,this,this.movingEvent);

                    stage.dispatchEvent(this.movingEvent); //Here is the place where error occurs on its second call


trace("b",stage,this,this.movingEvent);

           }

}