我希望能夠註冊觸發布爾變量的按鍵事件。我已經在我的主類中完成了,但是現在我正試圖將它放到類中,而且它似乎並沒有工作。鍵監聽器事件沒有觸發?
這裏是英雄職業:
public class Hero extends Main {
private boolean downPressed;
private boolean leftPressed;
private boolean rightPressed;
public void init() {
}
public void paint(Graphics g, int x_pos, int y_pos) {
if (isDownPressed() && isLeftPressed()) {
this.sprite = hero225;
} else if (isDownPressed() && isRightPressed()) {
this.sprite = hero135;
} else if (isUpPressed() && isRightPressed()) {
this.sprite = hero45;
} else if (isUpPressed() && isLeftPressed()) {
this.sprite = hero315;
} else if (isLeftPressed() == true) {
this.sprite = hero270;
} else if (isRightPressed() == true) {
this.sprite = hero90;
} else if (isUpPressed() == true) {
this.sprite = hero;
System.out.println("Succsess");
} else if (isDownPressed() == true) {
this.sprite = hero180;
}
// this.sprite will contain value set on last "movement"
g.drawImage(this.sprite, x_pos, y_pos, this);
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
setLeftPressed(true);
System.out.println("keyPressed");
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
setRightPressed(true);
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
setUpPressed(true);
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
setDownPressed(true);
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
setLeftPressed(false);
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
setRightPressed(false);
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
setUpPressed(false);
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
setDownPressed(false);
}
}
public void keyTyped(KeyEvent e) {
}
public void mouseClicked(MouseEvent e) {
System.out.println("HIT!");
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
boolean isButtonPressed = true;
}
public void mouseReleased(MouseEvent e) {
boolean isButtonPressed = false;
}
public void setDownPressed(boolean downPressed) {
this.downPressed = downPressed;
}
public boolean isDownPressed() {
return downPressed;
}
public void setLeftPressed(boolean leftPressed) {
this.leftPressed = leftPressed;
}
public boolean isLeftPressed() {
return leftPressed;
}
public void setRightPressed(boolean rightPressed) {
this.rightPressed = rightPressed;
}
public boolean isRightPressed() {
return rightPressed;
}
public void setUpPressed(boolean upPressed) {
this.upPressed = upPressed;
}
public boolean isUpPressed() {
return upPressed;
}
在此可以調用它的級別類:
public class Level extends Main {
Hero hero = new Hero();
public void paint(KeyEvent e, Graphics g, double x_pos, double x_pos2) {
repaint();
}
,這裏是在調用主類的塗料功能:
public void paint(Graphics g) {
Level level = new Level();
level.paint(e, g, x_pos, y_pos);
造成這種情況的問題似乎並不明顯。
我得到這個錯誤,當我按照第二步:方法addEventListener(英雄)是未定義的類型英雄 – Derek
我編輯了我的答案,所以你可以測試它 –
我設法讓它在英雄類工作(Woop!)但它在Level類中不起作用。我所做的是把一個System.out.println(「keyPressed」);在if(isDownPressed()&& isLeftPressed()){。但是當我加載了Level並按下左鍵時:控制檯中沒有任何事情發生: – Derek