对于InputHandler类型,未定义toggleKey(int,boolean)方法

时间:2013-08-23 21:50:54

标签: java input key

如果有人可以提供帮助,我就会徘徊,基本上我正在关注一些输入处理,并且教程要求我创建一个新类并编写此代码。

package net.pinkeye.JavaGame;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;

public class InputHandler implements KeyListener{

public InputHandler(Game game) {
    game.addKeyListener(this);
}

public class Key {
    public boolean pressed = false;

    public boolean isPressed(){
        return pressed;
    }
    public void toggle(boolean isPressed) {
        pressed = isPressed;
    }
}


public Key up = new Key();  
public Key down = new Key();    
public Key left = new Key();    
public Key right = new Key();

public void keyPressed(KeyEvent e) {
    toggleKey(e.getKeyCode(),true);
}


public void keyReleased(KeyEvent e) {
    toggleKey(e.getKeyCode(),false);
}

public void keyTyped(KeyEvent e) {
}


public void toggle (int keyCode, boolean isPressed) {
    if(keyCode == KeyEvent.VK_W){
        up.toggle(isPressed);
    }
    if(keyCode == KeyEvent.VK_S){
        down.toggle(isPressed);
    }
    if(keyCode == KeyEvent.VK_A){
        left.toggle(isPressed);
    }   
    if(keyCode == KeyEvent.VK_D){
        right.toggle(isPressed);
    }
}

}

然而它说的是

    public void keyPressed(KeyEvent e) {
    toggleKey(e.getKeyCode(),true);
}


public void keyReleased(KeyEvent e) {
    toggleKey(e.getKeyCode(),false);
}

eclipse告诉我方法toggleKey(int,boolean)未定义为InputHandler类型

我已经按照wrd的这个教程词进行了多次检查,但我找不到我的错误。

任何帮助都会很棒

如果这有帮助,请点击这里:http://www.youtube.com/watch?v=Vv7G5GMOre8

1 个答案:

答案 0 :(得分:3)

您的实际方法称为toggle,但您已声明可以调用toggleKey。将toggle重命名为toggleKey可以解决您的问题。