Hello,
I want to make a game where a ship shoots through 2 fireguns. The problem is that it doesn't shoot. When i code to shoot from the ship it's working but not from the fireguns and i don't have any compile errors. Here is the code:
//variables
var steps:Number=10;
var left:Boolean=false;
var right:Boolean=false;
var cTime:Number=0;
var cLimit:Number=12;
var allowShoot:Boolean=true;
//functions
ship.addEventListener(Event.ENTER_FRAME, moveShip);
function moveShip(event:Event):void {
if (left) {
ship.x-=steps;
}
if (right) {
ship.x+=steps;
}
if (ship.x<=0+ship.width) {
ship.x+=steps;
}
if (ship.x>=stage.stageWidth-ship.width) {
ship.x-=steps;
}
if (cTime<cLimit) {
cTime++;
} else {
allowShoot=true;
cTime=0;
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
function checkKeysDown(event:KeyboardEvent):void {
if (event.keyCode==37) {
left=true;
}
if (event.keyCode==39) {
right=true;
}
if (event.keyCode==97&&allowShoot) {
allowShoot=false;
var bulletR:Bullet=new Bullet();
bulletR.x=ship.gunR.x+ship.gunR.width/2-bulletR.width;
bulletR.y=ship.gunR.y;
addChild(bulletR);
}
if (event.keyCode==98&&allowShoot) {
allowShoot=false;
var bulletL:Bullet=new Bullet();
bulletL.x=ship.x+ship.width/2;
bulletL.y=ship.y;
addChild(bulletL);
}
/*if (event.keyCode==98&&allowShoot) {
allowShoot=false;
var bulletL:Bullet=new Bullet();
bulletL.x=ship.gunL.x+ship.gunL.width/2-bulletL.width;
bulletL.y=ship.gunL.y;
addChild(bulletL);
}*/
}
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUP);
function checkKeysUP(event:KeyboardEvent):void {
if (event.keyCode==37) {
left=false;
}
if (event.keyCode==39) {
right=false;
}
}
and here is the Bullet class:
package {
import flash.display.MovieClip;
import flash.events.*;
public class Bullet extends MovieClip {
var _root:Object;
var speed:Number=10;
public function Bullet() {
addEventListener(Event.ADDED, beginClass);
addEventListener(Event.ENTER_FRAME, eFrame);
}
public function beginClass(event:Event):void {
_root=MovieClip(root);
}
public function eFrame(event:Event):void {
y-=speed;
if (this.y<-1*this.height) {
removeEventListener(Event.ENTER_FRAME, eFrame);
_root.removeChild(this);
}
}
}
}