views:

250

answers:

2

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);
        }
    }
}
}
+1  A: 

The first piece of code, does it come out of a class? Or is it in your fla?

Have you checked with trace that Bullets are created? (and if so, that they are not immediately removed with _root.removeChild(this)?

Lieven Cardoen
The first piece of code is from the fla and yes bullets are created but it doesn'shoot. As you can see in the checkKeyDown function the "third if" is working fine, the ship is shooting, but from the gunR is not.
john
If you set allowShoot=false in your third if, then fourth if will will be false.
Lieven Cardoen
allowShoot becomes true again in the moveShip function and also just tu be sure i put another one in the checkKeysDown function outside the if's and still nothing.
john
the line if (this.y<-1*this.height), you do realise that "this" is relative to the Bullet Class and not the stage? I ask because I could see you removeing it after it traveled accross the screen, but to be remove after traveling negative its own height, well is confusing to me. Is the bullet supposed to shoot traveling upward, leave the stage, then be removed?
Brian Hodge
yes, the bullet is traveling only upward.
john
Have you tried leaving (or comment out) out the third if? Since code in third and fourth if is pretty much the same, maybe the fourth if won't work anymore if the third if is once executed.
Lieven Cardoen
In the end that fourth 'if' won't exist but it's there now because is showing me that the bullet class it's working. When i test play the game the bullet from ship is firing and the gunR's is not.If the 3rd 'if' is commented the bullet from the ship is working fine. When the 4th 'if' is commented and i remove the comment from the 5th 'if' there is no firing in the game.I hope you understand what i wrote now :))
john
A: 

ok an update

if (event.keyCode==97&&allowShoot) {
     allowShoot=false;
     var bulletR:Bullet=new Bullet();
     bulletR.x=ship.gunR.x;
     bulletR.y=ship.gunR.y;
     ship.addChild(bulletR);

adding the ship to the addChild the bullet is now appearing on stage but after a few pixels of moving the bullet is freezing and i get:

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. at flash.display::DisplayObjectContainer/removeChild() at Bullet/eFrame()

any ideas? thx

john
The error you get can happen when you want to remove something from a container that isn't in it, like '_root.removeChild(this);'. Maybe this isn't any longer a child of the _root, maybe the 'if (this.y<-1*this.height) {' statement is executed more than once... It's hard to debug this. I would need the fla and classes to look at it...
Lieven Cardoen
http://rapidshare.com/files/301981431/game.rar.html
john
Download session has expired... try again.
Lieven Cardoen
http://www.dump.ro/fisiere/game-rar/156102/FGWOztaNPMOTM9Gz
john