Search code examples
actionscript-3flash

EventDispatch error in action script 3


I am not sure if something is wrong with my code or if I have to handle the error differently. I have an instance of Gear and the other one is bLine. I am trying to make both move as soon as the gear falls on the line. So what I have inside of the instance coded is:

var ev2:Event = new Event("transfer");

dispatchEvent(ev2);


stop();

In the bLine I have: `

import flash.events.Event;

this.addEventListener(Event.ENTER_FRAME,Move);

function Move(e:Event):void {
    this.x=this.x+3;

};

Then, in the main timeline:

 import flash.events.Event;
gear.addEventListener("transfer",transferGear);

function transferGear(e:MouseEvent) {
bLine.gotoAndPlay(2);

 };

This is the output I'm getting: "TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::Event@16bbd6bb7821 to flash.events.MouseEvent. at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent()

at Rube2_fla::mc_gear_4/frame24()"

So what am I doing wrong and how I could fix it?


Solution

  • You can either change function parameter:

    function transferGear(e:Event) {...}
    

    Or event class you are dispatching.

    new MouseEvent("transfer");
    

    But in this case you are crating more additional field that you don't need (like local X)

    Most iteally you would extend Event class with your own and use define some custom static types to work like this:

    gear.addEventListener(MyOwesomeEvent.TRANSFER,transferGear);
    

    You may probably want to also see this question

    Suggestions

    If you want to do more advance stuff than simply play some animation or click a button then I would suggest to install some IDE and separate your code from your .fla file.

    Flash develop for example generate event listener code automatically when you hit ctrt + shift + 1 so it is hard to make a mistake. It something looks like this (not edited, system out of RAM... :/).

    enter image description here

    Also by convention we start function names from lower case letter so you can distinguish it from a class.