这是我的代码。
本质上,我创建了一个player
操纵动画的对象;但是通过公共静态方法GameController
发送命令。Player
但是,由于我keyPressed
的keyReleased
, 和keyTyped
不工作,我在这部分遇到了麻烦。
我输入了打印语句以检查是否KeyEvent
会显示任何内容,但没有。我还检查了我的代码,其中一个mouseListener
有效。因此,我不确定我做错了什么。
import objectdraw.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class GameController extends WindowController implements KeyListener, MouseListener {
Player player;
public GameController() {
super();
}
public void begin() {
Image sprite0 = getImage("1.png");
canvas.addKeyListener(this);
canvas.addMouseListener(this);
player = new Player (250, 0, sprite0 ,canvas);
}
public void keyPressed(KeyEvent e) {
System.out.println(e.getKeyChar());
}
public void keyReleased(KeyEvent e) {
System.out.println(e.getKeyChar());
}
public void keyTyped(KeyEvent e) {
}
public void mouseEntered( MouseEvent e ) { }
public void mouseExited( MouseEvent e ) { }
public void mousePressed( MouseEvent e ) {
if (Player.canJump && !Player.falling) {
Player.canJump = false;
Player.falling = false;
Player.jumping = true;
}
}
public void mouseReleased( MouseEvent e ) { }
public void mouseClicked( MouseEvent e ) {
}
}