0

我在一个类中有一个函数,可以将玩家一号移动 32 x 32 块。局部 x 和 y 值指的是 (1,0)、(0,1)、(-1,0) 或 (0,-1)(指运动方向)。sprites 是一个缓冲图像数组,引用不同的行走精灵。Player.x 和 Player.y 是 Player 的当前 tile 坐标,miniX 和 miniY 是图像移动的像素量。Swing.player 是一个包含图像的 JPanel。currentSprite 是当前使用的尽管。

        switch (2 * x + y) {
            case 2:
                Player.miniX = 4;
                currentSprite = sprites[10];
                Swing.player.repaint();
                //pause
                Player.miniX = 8;
                currentSprite = sprites[11];
                Swing.player.repaint();
                //pause
                Player.miniX = 12;
                Swing.player.repaint();
                currentSprite = sprites[10];
                Swing.player.repaint();
                //pause
                Player.miniX = 16;
                currentSprite = sprites[11];
                Swing.player.repaint();
                //pause
                Player.miniX = 20;
                currentSprite = sprites[10];
                Swing.player.repaint();
                //pause
                Player.miniX = 24;
                currentSprite = sprites[11];
                Swing.player.repaint();
                //pause
                Player.miniX = 28;
                currentSprite = sprites[10];
                Swing.player.repaint();
                //pause
                Player.miniX = 0;
                Player.x += 1;
                currentSprite = sprites[9];
                break;
            case 1:
                //omitted as is same as above
                break;
            case -1:
                //omitted as is same as above
                break;
            case -2:
                //omitted as is same as above
                break;
        }

在每个班次之间暂停几毫秒的最佳方式是什么?

4

2 回答 2

0

用这个 :Thread.sleep(1000);

这将使您当前正在执行的线程休眠 1 秒,即 1000 毫秒

于 2013-03-23T21:18:42.990 回答
0

Use a Swing Timer to schedule the animation. Read the section from the Swing tutorial on How to Use Timers.

于 2013-03-23T21:26:23.930 回答