i am currently creating a shooting game , currently i have a main class which is the place where i add “main childs” at, and i have a object class “gunner”, i added that “gunner” to my main, and i have a eventListener,MOUSE_CLICK on the gunner class, however when i did a click nothing happens, why is that so? in my case the gunner is supposed to shoot bullets. is the event not being dispatched to my gunner object?
constructor of my main class.
public function waterMain()
{
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
canPlay = true;
soundTimer = new Timer(300, 1);
soundTimer.addEventListener(TimerEvent.TIMER, waterSoundHandler, false, 0, true);
waterMovementSound = new pondSound();
bg = new Background(stage);
myRippler = new Rippler(bg , 20, 5, 5);
//event listeners
addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
addEventListener(TouchEvent.TOUCH_MOVE, onTouchBegin);
addEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveTriggered);
addChild(bg);
touchPoints = Multitouch.maxTouchPoints;
if (Multitouch.supportsGestureEvents && Multitouch.supportsTouchEvents)
{
trace("Multitouch gesture supported");
var inputField:TextField = new TextField();
inputField.border = true;
inputField.wordWrap = true;
inputField.text = "Multitouch gesture,touch supported,MTP : "+touchPoints;
addChild(inputField);
}else if (Multitouch.supportsTouchEvents)
{
trace("touch supported");
var inputField2:TextField = new TextField();
inputField2.border = true;
inputField2.wordWrap = true;
inputField2.text = "Multitouch touch supported,MTP : "+touchPoints;
addChild(inputField2);
}
//game
//creating gunner
var gun:gunner = new gunner(stage);
gun.x = stage.stageWidth / 2;
gun.y = stage.stageHeight;
addChild(gun);
}
gunner class
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 function gunner(stageRef:Stage)
{
_stageRef = stageRef;
addEventListener(Event.ENTER_FRAME, rotateTurret);
addEventListener(MouseEvent.CLICK, fireBullet);
}
private function rotateTurret(evt:Event):void
{
//calculations, distance, angle etc
var a:Number = _stageRef.mouseX - this.x;
var b:Number = _stageRef.mouseY - this.y;
var angRad:Number = Math.atan2(b, a);
var angDeg:Number = (angRad * 180 / Math.PI);
rotation = angDeg;
}
private function fireBullet(mouseEvt:MouseEvent):void
{
_stageRef.addChild(new bullet(_stageRef, 500,500));
}
}
}
bullet class
public class bullet extends MovieClip
{
//variables
private var _bulletSpeedVector:Point;
private var _stageRef:Stage;
private var _maxX:Number = 1024;
private var _maxY:Number = 768;
public function bullet(stageRef:Stage,x:Number,y:Number)
{
_stageRef = stageRef;
this.x = x;
this.y = y;
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
private function loop(e:Event) : void
{
//move bullet
/*if (this.x > _maxX || this.x < 0)
removeSelf();
else if (this.y > _maxY || this.y < 0)
removeSelf();
*/
}
private function removeSelf() : void
{
removeEventListener(Event.ENTER_FRAME, loop);
if (_stageRef.contains(this))
_stageRef.removeChild(this);
}
}
i am not sure why this happens, but when i changed it to MouseEvent.MOUSE_DOWN, it works. but with click it cant work.