0

因此,在我的小型横向卷轴 Java 游戏中,我试图让我的角色与墙壁发生碰撞。我使用键绑定而不是键侦听器,因为它经常引起聚焦问题。这是我遇到问题的代码:

 Action moveleft = new AbstractAction(){
      public void actionPerformed(ActionEvent e)
      {
          if (moving==true&&offsetX!=2){
              offsetX=offsetX+2;
          }
      }
    };

问题是当按住某个键使角色移动时,在 actionperformed 方法中,它不会重新检查确定该键按下时移动是真还是假的条件语句。这会导致角色无限期地穿过所有墙壁,直到释放钥匙。

那么这是为什么呢?我能做些什么来补救它?如果我的程序需要更多代码,我可以提供!谢谢!

这是我的面板类的精简版,基本上所有的碰撞都会发生:

public class Panel extends JPanel implements ActionListener
{
    public static boolean moving=true;
    Player p1;
    public InputMap inputmap;
    public ActionMap actionmap;
    ArrayList<Level> Levels;

    Level level1;
    protected static int offsetX=0;
    protected static int offsetY=0;

    public Panel() {
        inputmap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
        inputmap.put(KeyStroke.getKeyStroke("D"), "startright");
        inputmap.put(KeyStroke.getKeyStroke("A"), "startleft");
        inputmap.put(KeyStroke.getKeyStroke("released D"), "stop");
        inputmap.put(KeyStroke.getKeyStroke("released A"), "stop");
        actionmap = getActionMap();
        actionmap.put("startright", moveright);
        actionmap.put("startleft", moveleft);
        actionmap.put("startup", moveup);
        actionmap.put("startdown", movedown);
        actionmap.put("stop", stop);
    }
    //The game's main game loop
    @Override
    public void actionPerformed(ActionEvent evt) {
        if (!running){
            startGame();
        }
        gameUpdate();
        repaint();
    }
    //Updates the game every iteration
    private void gameUpdate() {
        p1.update();
        for (int i = 0; i < Levels.size(); i++) {
            collide(p1, level1.Blocks.get(i));
        }
    }

    Action moveright = new AbstractAction()
    {
        public void actionPerformed(ActionEvent e)
        {
            //p1.setRight(e);
            if (moving==true&&offsetX!=-2){
                offsetX=offsetX-2;
            }
        }
    };

    Action moveleft = new AbstractAction()
    {
        public void actionPerformed(ActionEvent e)
        {
            //p1.setLeft(e);
            if (moving==true&&offsetX!=2){
                offsetX=offsetX+2;
            }
        }
    };

    Action moveup = new AbstractAction()
    {
        public void actionPerformed(ActionEvent e)
        {
            //p1.setUp(e);
            if (moving==true&&offsetY!=-1){
                offsetY=offsetY-1;
            }
        }
    };

    Action movedown = new AbstractAction()
    {
        public void actionPerformed(ActionEvent e)
        {
            //p1.setDown(e);
            if (moving==true&&offsetY!=1){
                offsetY=offsetY+1;
            }
        }
    };

    Action stop = new AbstractAction()
    {
        public void actionPerformed(ActionEvent e)
        {
            //p1.stop(e);
            offsetX=0;
            offsetY=0;
        }
    };


    public void collide(Player p, Block b) {
        if (Math.abs(p.minX) < Math.abs(b.maxX)) {
            moving=false;   
        }
        else {
            //moving=true;
        }
    }
}

这基本上是所有涉及碰撞的事情。我真的只是想知道为什么即使在moving == false.

4

0 回答 0