简体   繁体   中英

Array elements not being set correctly

Hi I have an array of bullet objects.I have the bullet objects isAlive var set to false. If I run the code without clicking the space button(which fires the bullet) and print out the array objects isAlive var and its prints them all false(as it should).

If I click the space button once the bullet appears on screen and moves correctly but when I print out the bullet array showing the isAlive var they are all set to true with the same x/y positions.If I click the space button twice 2 bullets appear and travel normally but again when I print the array it shows the first ten elements as alive,with the same x/y positions(the first bullet on the screen) and the next ten are also alive with the same x/y positions(2nd bullet on screen)

any more presses of the space bar doesnt add new bullets. here;'s the code for the fireButton()

public void fireBullet(){
    if(currentBullet > BULLETS -1){
        currentBullet = 0;
    }
    if(bullet[currentBullet].isAlive()==false){
        bullet[currentBullet].setAlive(true);
        bullet[currentBullet].setxPos(ship.getxPos());
        bullet[currentBullet].setyPos(ship.getyPos());
    }
    currentBullet ++;
}

and here's were its called in the KeyPressedMethod

case KeyEvent.VK_SPACE: 
        fireBullet();
        break;

can anyone see where I'm going wrong?Being messing with the code for a while now and its doing my head in :)

EDIT:I'll answer all the questions if I can here :) 1)currentBullet is a private class member and is initialised to 0 at first,and is then incremented in the fireBullet() method for each bullet fired up to the 20 and is then reset to 0.

2)when I iniatilise the bullet array I assign a new Bullet() to each element.Each bullet constructor sets isAlive to false

3)the bullet var isAlive is set back to false when it leaves the screen 4) here's the keypressed Method in full

public void keyPressed(KeyEvent k) {
    int keyCode = k.getKeyCode();

    switch (keyCode) {

    case KeyEvent.VK_A:
        //move ship left
        if(ship.getxPos()<20){
            ship.setxPos(20);
        }else
        ship.setxPos(ship.getxPos()-1);
        break;
    case KeyEvent.VK_D:
        if(ship.getxPos()>1260){
            ship.setxPos(1260);
        }else
        ship.setxPos(ship.getxPos()+1);
        break;
    case KeyEvent.VK_SPACE: 
        fireBullet();
        break;
    }


}//end keypressed event

I think the rendering is ok as it is drawing the bullets but drawing 10 or twenty of them on top of each other so when the code is ran you only see two bullets moving up the screen,

The problem is that there should only be 1 alive bullet per space bar press but by pressing it once all 20 bullets are being set to alive!

2nd EDIT:I print out current bullet at end of game loop and it seems to be incrementing correctly which indicates the fireBullet()method is being called on every spacebar press,just seems to be filling the array with alive bullets for every press

Thanks all got it working!Like NPE suggested above I put in a small println in fireBullet() methosd and also in the other 2 key presses.What was happening was while i was hitting space once the code was picking up loads of 'hits'. Turned out I had addKeyListener(this) in my paint method and not in the JFrame constructor,honestly dont know how it got put in the paint method :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM