2010-05-27 14 views

回答

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(); 
    } 


} 
+0

我該怎麼辦呢? – nn2 2010-05-27 21:51:10

+0

您可以在tileGen類,lastX和lastY中聲明兩個新的成員字段,並相應地使用它們。更新後,您將設置lastX和lastY,並在輸入更新時檢查lastX和lastY的值。 – 2010-05-27 21:54:33

+0

成員字段在INT字段中?但是,我說...我如何檢測lastX和lastY? – nn2 2010-05-27 22:18:11