OK so, is there an efficient way to detect on what array you're currently on by using the KeyListener?
My code: http://www.javadan.pastebin.com/X68VyuGL
What I am trying to do here is see if the current tile I am on is BLOCKED.
Thanks.
OK so, is there an efficient way to detect on what array you're currently on by using the KeyListener?
My code: http://www.javadan.pastebin.com/X68VyuGL
What I am trying to do here is see if the current tile I am on is BLOCKED.
Thanks.
I'm guessing you don't want to have to traverse the entire matrix every time to find out what tile the users on after a key event. Why not keep the state of the last position of the player on the board and use it when you're updating for each key event that occurs?
Alright, you wanted some more information on how to do this, I'll seed the answer for you and you can finish it :
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();
}
}