[英]How to move object left, right, up and down using KeyListener does not seem to be working
尝试使用向左、向右、向上和向下箭头让我的播放器移动,有一个名为 InputHandler 的类,它实现了 KeyListener 并在 KeyPressed 中有代码,例如 VK_LEFT 等,尝试使用 Switch 和 If Else 语句,也许我有点愚蠢
Method to handle key presses captured by the GameGUI. The method calls
* the game engine doTurn method to process a game turn for ANY key press,
* but if the up, down, left or right arrow keys are pressed it also calls a
* method in the engine to update the game by moving the player.
*
* @param e A KeyEvent object generated when a keyboard key is pressed
**
* Handles the movement of the player when attempting to move in the game.
* This method is automatically called by the InputHandler class when the
* user has presses one of the arrow keys on the keyboard. The method should
* check which direction for movement is required, by checking which
* character was passed to this method (see parameter description below). If
* the tile above, below, to the left or to the right is clear then the
* player object should have its position changed to update its position in
* the game window. If the target tile is not empty then the player should
* not be moved, but other effects may happen such as giving a customer
* food, or picking up food etc. To achieve this, the target tile should be
* checked to determine the type of tile (food, table, wall etc.) and
* appropriate methods called or attribute values changed.
@param direction A char representing the direction that the player should
* move. U is up, D is down, L is left and R is right.
这些是我被告知要在代码中实现的内容,但箭头键似乎不会让玩家移动
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_LEFT:
engine.movePlayer('L');
break; //handle left arrow key
case KeyEvent.VK_RIGHT:
engine.movePlayer('R');
break;//handle right arrow
case KeyEvent.VK_UP:
engine.movePlayer('U');
break; //handle up arrow
case KeyEvent.VK_DOWN:
engine.movePlayer('D');
break; //handle down arrow
}
engine.doTurn();
上面的这段代码是我被告知要编辑的代码,这样当按下左箭头时玩家就会向左移动,上面的代码在 InputHandler 类中
public void movePlayer(char direction) {
switch (direction) {
case 'U':
// update player position and game state to move player up
break;
case 'L':
// update player position and game state to move player left
break;
case 'R':
// update player position and game state to move player right
break;
case 'D':
// update player position and game state to move player down
break;
default:
// code to execute if direction is none of the above
break;
}
上面的代码是我添加的代码,上面的代码在GameEngine类中
现在,每当我尝试运行这个项目时,都没有编译器错误,但箭头键似乎不会移动播放器
您需要显示GameEngine
类的代码。 如果它包含玩家在屏幕上的x
和y
位置,则需要相应地更新它。
例如,如果你有玩家的x
和y
坐标(你应该考虑到你需要移动玩家),你可以执行以下操作:
public void movePlayer(char direction) {
switch (direction) {
case 'U':
// update player position and game state to move player up
y -= 1;
break;
case 'L':
// update player position and game state to move player left
x -= 1;
break;
case 'R':
// update player position and game state to move player right
x += 1;
break;
case 'D':
// update player position and game state to move player down
y += 1;
break;
default:
// code to execute if direction is none of the above
break;
}
这是一个非常基本的实现,没有考虑速度和摩擦,并假设坐标系的(0,0)
从屏幕的左上角开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.