如何检测数组

发布于 2024-09-03 06:06:10 字数 232 浏览 3 评论 0原文

好的,有没有一种有效的方法可以使用 KeyListener 检测您当前所在的数组?

我的代码: http://www.javadan.pastebin.com/X68VyuGL

我正在尝试这里要做的是查看我所在的当前图块是否被阻止。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罪#恶を代价 2024-09-10 06:06:10

我猜您不想每次都遍历整个矩阵来找出关键事件后用户使用的图块。为什么不保留棋盘上玩家最后位置的状态,并在更新发生的每个关键事件时使用它?

好吧,您想要更多有关如何执行此操作的信息,我将为您提供答案,您可以完成它:

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


 }

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


 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文