Reputation: 550
I made a door/button and added as a class, i want it so when you click on it, it dispatches an event that signals a function in the main actionscript page to run
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Door extends MovieClip {
var direct:String;
var doorId:int;
var dispatcher = new EventDispatcher();
public function Door() {
this.addEventListener(MouseEvent.CLICK, changeRoom)
this.buttonMode = true;
}
function changeRoom(e:MouseEvent):void{
trace("door click")
dispatcher.dispatchEvent( new Event("doorClick") );
}
}
}
the main page looks like this
var doorTop = new Door();
doorTop.addEventListener("doorClick",goToRoom);
//should i be doing stage.addEventListener("doorClick",goToRoom);
function goToRoom(e:Event):void
{
trace("i went here")
}
what am i doing wrong? seems straight forward enough
Upvotes: 1
Views: 5946
Reputation: 8149
You aren't dispatching the event from doorTop
, you're dispatching it from dispatcher
within doorTop
.
Door
extends MovieClip
which extends EventDispatcher
. You don't need to create an EventDispatcher, your Door
class already is one.
Just do this:
function changeRoom(e:MouseEvent):void{
trace("door click")
this.dispatchEvent( new Event("doorClick") );
}
That will dispatch it from the Door
object.
Additionally, if you are using classes, every single object (including functions) declared within the top-level scope of a class must include an access modifier (public, private, internal, or protected). You cannot just declare function changeRoom
, it must be public function changeRoom
. Same with each of your variables.
Upvotes: 4