好的,有没有一种有效的方法可以使用KeyListener检测您当前在哪个阵列上?
我的代码:http://www.javadan.pastebin.com/X68VyuGL
我在这里要做的是查看我当前打开的牌是否已被阻止。
感谢。
答案 0 :(得分:2)
我猜你不想每次都要遍历整个矩阵,以找出关键事件后用户的内容。为什么不保持播放器最后一个位置的状态并在更新每个发生的关键事件时使用它?
好的,你想了解更多关于如何做到这一点的信息,我会给你答案,你可以完成它:
public class tileGen extends Applet implements KeyListener {
Image[] tiles; // tile arrays
Image player; // player image
int x, y, px, py, tx, ty; // x tile - y tile // player x - player y // tile x - tile y
boolean left, right, down, up, canMove; // is true?
int[][] board; // row tiles for ultimate mapping experience!
final int NUM_TILES = 25; // how many tiles are we implementing?
Label lx, ly; // to see where we are!
public final int BLOCKED = 1;
int lastX, int lastY; // <=== new member fields to track last X and last Y
public void keyPressed(KeyEvent e) {
// UPDATE LAST X AND LAST Y IN THIS METHOD
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
left = true;
px = px - 32;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
right = true;
px = px + 32;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
down = true;
py = py + 32;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
up = true;
py = py - 32;
}
repaint();
}
}