Copy link to clipboard
Copied
i'm trying to shoot a cannon upon pressing the space key. This is my code, i'm not entirely sure why this isnt working as i have it working on a similar way elsewhere. With the current code it will spawn a missile, but just where the tank is, and not propelling forward.
tank2.addEventListener(Event.ENTER_FRAME, controlGuns);
addEventListener(Event.ENTER_FRAME, gameUpdate);
stage.addEventListener(KeyboardEvent.KEY_DOWN, shootGuns);
addEventListener(Event.ENTER_FRAME, moveMissiles);
stage.focus = stage;
var missileCounts = 0;
function controlGuns(evt: Event) { //Control the gun on only the X axis.
tank2.x = mouseX;
}
function shootGuns(evt: KeyboardEvent) { //Shoot the gun using a switch statement
switch (evt.keyCode) {
case Keyboard.SPACE:
var newMissiles = root["missile" + missileCounts] = this.addChild(new Missile());
newMissiles.x = tank2.x;
newMissiles.y = tank2.y;
newMissiles.width = 15;
newMissiles.height = 15;
var cannonSounds = new CannonSound(); //When shooting, play the audio file
break;
}
}
function moveMissiles(evt: Event) {
for (var i = 1; i <= missileCounts; i++) { //Allow each individual missile to detect if it has hit the ship
root["missile" + i].y += -25;
if (root["missile" + i].hitTestObject(shipArray)) {
}
}
}
Copy link to clipboard
Copied
i don't see where you're updating missleCounts. typically that would be done in shootGuns:
function shootGuns(evt: KeyboardEvent) { //Shoot the gun using a switch statement
switch (evt.keyCode) {
case Keyboard.SPACE:
var newMissiles = root["missile" + missileCounts] = this.addChild(new Missile());
missleCounts++
and you need to fix your for-loop
for (var i = 1; i < missileCounts; i++)
Find more inspiration, events, and resources on the new Adobe Community
Explore Now