i am having trouble understanding how to dispatch events and where to catch them. currently i have a document class waterMain, and another class called gunner. my waterMain createds a object of gunner and adds it as child, in my gunner there is a function firebullet, in it i dispatch a
dispatchEvent(new Event(“bulletFired”));
and from my waterMain class i do a
addEventListener("bulletFired", bulletFiredHandler);
private function bulletFiredHandler(e:Event):void
{
hudMenu.reduceBullet();
}
the hudMenu is a variable on the waterMain document class, however it seems that nothing is happening. is the waterMain class not catching the dispatched event?
package
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
/**
* ...
* @author ...
*/
public class Gunner extends MovieClip
{
private var _stageRef:Stage;
public var barrel:MovieClip;
public var point:MovieClip;
public var _hudMenu:HUD;
public function Gunner(stageRef:Stage)
{
//trace("current rotation" + barrel.rotation);
//barrel.rotation = -90;
_stageRef = stageRef;
addEventListener(Event.ENTER_FRAME, rotateTurret);
_stageRef.addEventListener(MouseEvent.MOUSE_DOWN, fireBullet);
private function rotateTurret(evt:Event):void
{
//calculations, distance, angle etc
var a:Number = _stageRef.mouseX - x;
var b:Number = _stageRef.mouseY - y;
var angRad:Number = Math.atan2(b, a);
var angDeg:Number = (angRad * 180 / Math.PI);
//trace(angDeg );
barrel.rotation = (angDeg);
}
private function fireBullet(mouseEvt:MouseEvent):void
{
dispatchEvent(new Event("bulletFired"));
_stageRef.addChildAt(new Bullet(_stageRef,x, y , new Point(mouseEvt.stageX, mouseEvt.stageY), _koiArray, _lionArray), 1);
_stageRef.addChild(_stageRef.addChild(this));
}
}
}
You need to add the event to the gunner class something like this:
The gunner class is already a EventDispatcher subclass, if I read your question correctly.