从键盘输入更新更新程序方法不喜欢方法调用?

时间:2018-05-31 13:52:26

标签: java class keylistener

我又回来了。

我正在尝试使用键盘命令移动一个盒子,但它似乎不应该如何工作。这是代码:

游戏类

import java.applet.Applet;
import java.awt.Color;
import java.awt.Event;
import java.awt.Graphics;


public class Game extends Applet
{
    Thread loopThread;
    boolean left  = false;
    boolean right = false;
    boolean up = false;
    boolean down = true;
    int x = 0;
    int y = 0;
    int tick = 0;
    int[][] board = new int[12][12];
    Graphics g;
    Player p = new Player(0,0);

    public void run() 
    { 

        long startTime;

        if (loopThread == null) 
        {
            loopThread = new Thread();
            loopThread.start();
        }


        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
        startTime = System.currentTimeMillis();
        while(Thread.currentThread() == loopThread) 
        {
            update(g);
        }
    }


    public void update(Graphics g) 
    {

         System.out.println("repaint");
         if(left)
         {
             p.goL();
             p.drawPlayer();
             left = false;
         }
         else if(right)
         {
             p.goR();
             p.drawPlayer();
             right = false;
         }
         else if(up)
         {
             p.goU();
             p.drawPlayer();
             up = false;
         }
         else if(down)
         {
             p.goD();
             p.drawPlayer();
             down = false;
         }
         else
         {
             p.drawPlayer();
         }

    }


    public boolean keyDown(Event e, int key) 
    {
            if (key == Event.LEFT) {
                left = true;
            }

            if (key == Event.RIGHT) {
                right = true;
            }

            if (key == Event.UP) {
                up = true;
            }

            if (key == Event.DOWN) {
                down = true;
            }

            repaint();
            return true;
     }
}

玩家类

  import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Event;
    import java.awt.Graphics;

public class Player
{
    private int x;
    private int y;
    private int[][] b;
    Graphics g;

    public Player(int xPos, int yPos)
    {
        int x = xPos;
        int y = yPos;
    }

    public void drawPlayer()
    {
        g.setColor(new Color(0,0,0));
        g.fillRect(40*x, 40*y, 40, 40);
    }

    public int getX()
    {
        return x;
    }

    public int getY()
    {
        return y;
    }

    public void goL()
    {
        x--;
    }

    public void goR()
    {
        x++;
    }

    public void goU()
    {
        y++;
    }

    public void goD()
    {
        y--;
    }

}

之前我将绘图方法放在更新类中的if语句之前(我仍然可以将绘图方法放在那里,并在按下相应的键时触发),但是当我尝试引用时在绘制的不同类中的方法,它似乎没有反应。我不确定更新方法和关键命令是否存在问题(这对我来说是新的),或者我的Player类是否存在根本错误,使得按预期工作。

任何输入都有帮助,谢谢! (另外,我想使用Applet,而不是Jframe,这是一个偏好)

1 个答案:

答案 0 :(得分:0)

我发现如果您将drawPlayer方法更改为:

public void drawPlayer(Graphics g)
{
    g.setColor(new Color(0,0,0));
    g.fillRect(40*x, 40*y, 40, 40);
}

然后只需从Graphics类传递Game对象:

public void update(Graphics g) 
{
     System.out.println("repaint");
     if(left)
     {
         p.goL();
         p.drawPlayer(g);
         left = false;
     }
     else if(right)
     {
         p.goR();
         p.drawPlayer(g);
         right = false;
     }
     else if(up)
     {
         p.goU();
         p.drawPlayer(g);
         up = false;
     }
     else if(down)
     {
         p.goD();
         p.drawPlayer(g);
         down = false;
     }
     else
     {
         p.drawPlayer(g);
     }

}

框应绘制。虽然,出于某种原因,我必须首先点击Applet才能开始绘图,然后如果你调整Applet的大小,那么绘图就会消失。

此外,作为一个简单的修复,如果您不想要前一个框位置的轨迹,只需创建两个变量来跟踪前一个框位置然后清除它。例如:

private int lastX;
private int lastY;

public void drawPlayer(Graphics g)
{
    g.clearRect(40*lastX, 40*lastY, 40, 40);
    g.setColor(new Color(0,0,0));
    g.fillRect(40*x, 40*y, 40, 40);
    lastX = x;
    lastY = y;
}