在更新KeyListener之前,对象尚未完全创建

时间:2015-06-04 20:34:28

标签: java keylistener

我正在制作基本的平铺游戏。在我的游戏中,当玩家点击空格键时,会创建一个火球。但是,直到玩家按下另一个键才能完全创建火球。换句话说,如果玩家按住空格键wile hold,D,则火球将悬停在他们的身体上,直到他们点击另一个键或释放D键。

我在fireball类中使用了测试代码来查看该类是否已初始化,但是在KeyListener更新之前,传递给它的值不会更新(我认为)。为什么这样,我该如何解决?我只是想让它成为第二个空格键被点击火球被创建。

玩家类:

public class Player extends MapObject{

//player stuff
private double health;
private int maxHealth;
private double mana;
private int maxMana;
private int level;
private boolean alive;
private Rectangle boundingBox;
private int direction;

private ArrayList<Integer>keyPressed;

//fireball stuff
private boolean firing;     //used to make sure player only used one attack at a time;
private boolean fireAttack; //used to limit fire speed
private int fireCost;
private int fireDamage;
private ArrayList<FireBall> fireBall;
private FireBall fireball;

public Player() {

    x = GamePanel.WIDTH / 2.1;
    y = GamePanel.HEIGHT / 2.2;

    width = 16;
    height = 16;
    boundingBox = new Rectangle ((int)x, (int)y, width, height);

    moveSpeed = 4;
    direction = 0;

    keyPressed = new ArrayList<Integer>();

    level = 1;
    maxHealth = level * 100;
    maxMana = level * 85;

    fireDamage = level * 5;
    fireBall = new ArrayList<FireBall>();

    init();
}

public void init() {

    health = maxHealth;
    mana = maxMana;

    alive = true;

    firing = false;
    fireAttack = true;
    fireCost = 10;

}


public void draw(Graphics2D g) {

    g.drawImage(Assets.player, (int)x, (int)y, null);

}
public void update() {

    if(health == 0) 
        alive = false;
    mana = mana + 0.04;
    health = health + 0.015;
    if(mana >= maxMana)
        mana = maxMana;
    if(health >= maxHealth)
        health = maxHealth;
    if(keyPressed.contains(KeyEvent.VK_W))
        direction = 0;
    if(keyPressed.contains(KeyEvent.VK_D))
        direction = 2;
    if(keyPressed.contains(KeyEvent.VK_A))
        direction = 1;
    if(keyPressed.contains(KeyEvent.VK_S))
        direction = 3;
}

public void keyPressed(int k) {

    if(k == KeyEvent.VK_SPACE) {
        if(fireAttack) {
            if(mana >= fireCost) {
                mana -= fireCost;
                fireball = new FireBall(x, y, direction);
                fireBall.add(fireball);
                fireAttack = false;
            }
        }

    }

    if(!keyPressed.contains(k)) keyPressed.add((k));



    for(int i = 0; i < fireBall.size(); i++) {
        fireBall.get(i).keyPressed(k);
    }


}
public void keyReleased(int k) {

    keyPressed.remove(new Integer(k));
    if(k == KeyEvent.VK_SPACE) 
        fireAttack = true;
    for(int i = 0; i < fireBall.size(); i++) {
        fireBall.get(i).keyReleased(k);
    }

}

火球类:

public class FireBall extends MapObject{

private double x, y;
private double movespeed, velX, velY;
private int direction;

private double xOffset, yOffset;
private boolean oUp = true, oDown = true, oLeft = true, oRight = true;

public void setoUp(boolean oUp) {
    this.oUp = oUp;
}


public void setoDown(boolean oDown) {
    this.oDown = oDown;
}


public void setoLeft(boolean oLeft) {
    this.oLeft = oLeft;
}


public void setoRight(boolean oRight) {
    this.oRight = oRight;
}

private ArrayList<Integer>keyPressed;

private Rectangle boundingBox;

private int fireCost;
private int fireDamage;

private boolean delete = false;


public FireBall(double x, double y, int direction) {

    this.x = x; //default positions at player location
    this.y = y;
    movespeed = 4;
    this.direction = direction;
    xOffset = 0;
    yOffset = 0;

    width = Assets.fireball1.getWidth();
    height = Assets.fireball1.getHeight();
    boundingBox = new Rectangle((int)this.x, (int)this.y, width, height);

    keyPressed = new ArrayList<Integer>();
}


public void update() {

    x = xOffset + 150;
    //xOffset += velX;    note: this was commented out as part of my testing. Without 
                          //the fireball moving it is obvious that if follows the 
                          //player at first.
    y = yOffset  + 100;
    //yOffset += velY;
    boundingBox.x = (int)x;
    boundingBox.y = (int)y;

    switch(direction) {

        case 0: velY = -movespeed;
                velX = 0;
                break;
        case 1: velY = 0;
                velX = -movespeed;
                break;
        case 2: velY = 0;
                velX = movespeed;
                break;
        case 3: velY = movespeed;
                velX = 0;
                break;

    }

    movement();

}
public void draw(Graphics2D g) {

    g.drawImage(Assets.fireball1, (int)x, (int)y, null);

}

public void keyPressed(int k) {

    if(!keyPressed.contains(k)) keyPressed.add((k));

}

public void movement() {

    if(keyPressed.contains(KeyEvent.VK_W) && oUp == true) {
        yOffset = yOffset + 2;                                  //PUT IN PLAYER MOVEPSEED MAY NEED TO BE CHANGED LATER
    }
    if(keyPressed.contains(KeyEvent.VK_D) && oRight == true) {
        xOffset = xOffset -2;   
    }
    if(keyPressed.contains(KeyEvent.VK_A) && oLeft == true) {
        xOffset = xOffset + 2;  
    }
    if(keyPressed.contains(KeyEvent.VK_S) && oDown == true) {
        yOffset = yOffset - 2;  
    }

}

public void keyReleased(int k) {
    keyPressed.remove(new Integer(k));
}

非常感谢您提供的任何帮助!

1 个答案:

答案 0 :(得分:0)

您是否尝试将keyPressed(int keyEvent)中的所有内容放入keyTyped(int keyEvent)?从技术上讲,只要按下一个键,键入一个键就会按下并抬起手指。 这可能对你有帮助。祝你好运!

旁注:你用这些钥匙做的很奇怪。为什么您的班级没有实施KeyListener界面?