简体   繁体   English

数组元素设置不正确

[英]Array elements not being set correctly

Hi I have an array of bullet objects.I have the bullet objects isAlive var set to false. 嗨,我有一个子弹对象数组。我将子弹对象isAlive var设置为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). 如果我运行代码时未单击空格键(会发射子弹)并打印出数组对象isAlive var,则将它们全部打印为false(应按要求进行打印)。

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) 如果我单击空格按钮,一旦项目符号出现在屏幕上并且可以正确移动,但是当我打印出显示isAlive var的项目符号数组时,它们都设置为true,并且具有相同的x / y位置。如果单击两次空格按钮,则2个项目符号出现并正常行进,但再次打印数组时,它显示前十个元素处于活动状态,具有相同的x / y位置(屏幕上的第一个项目符号),而后十个元素也处于活动状态,并且具有相同的x / y位置(屏幕上的第二个项目符号)

any more presses of the space bar doesnt add new bullets. 再按一下空格键不会添加新的项目符号。 here;'s the code for the fireButton() 这是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 这就是它在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. 编辑:如果可以的话,我会回答所有问题:) 1)currentBullet是私有类成员,首先被初始化为0,然后在fireBullet()方法中对每个发射到20的子弹进行递增然后重置为0。

2)when I iniatilise the bullet array I assign a new Bullet() to each element.Each bullet constructor sets isAlive to false 2)当我使用子弹数组时,我为每个元素分配一个新的Bullet()。每个子弹构造函数将isAlive设置为false

3)the bullet var isAlive is set back to false when it leaves the screen 4) here's the keypressed Method in full 3)项目符号var isAlive离开屏幕时将其设置回false 4)这是完整的按键方法

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, 我认为渲染是可以的,因为它可以绘制项目符号,但可以相互叠加绘制10或20个项目,因此在运行代码时,您只会看到两个项目符号在屏幕上移动,

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! 问题在于,每按一次空格键,应该只有1个活着的子弹,但是一旦将所有20个子弹都设置为活着,就按下它。

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 第二编辑:我在游戏循环结束时打印出当前的项目符号,它似乎正在正确地递增,这表明在每次按下空格键时都会调用fireBullet()方法,似乎每次按下时都会用活跃的项目符号填充数组

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'. 就像上面提到的NPE一样,我在fireBullet()methosd以及其他2个按键中放入了一个小println。发生的是,当代码拾取了很多``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 :) 原来我在我的paint方法中有addKeyListener(this),而不是在JFrame构造函数中,诚实的不知道它是如何放入paint方法中的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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