有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

二维地图平铺java上的块碰撞问题

我有撞墙的问题。基本上,我想让我的球员在遇到拦网时停下来。 以下是我到目前为止所做的:

Keylistener设置:

addKeyListener(new KeyAdapter(){
        public void keyPressed(KeyEvent e){
            if(e.getKeyCode() == KeyEvent.VK_A){
                pressL = true;  

            }

            if(e.getKeyCode() == KeyEvent.VK_D){
                pressR = true;  

            }

            if(e.getKeyCode() == KeyEvent.VK_W){
                pressU = true;  

            }

            if(e.getKeyCode() == KeyEvent.VK_S){
                pressD = true;  

            }
        }

        public void keyReleased(KeyEvent e){
            if(e.getKeyCode() == KeyEvent.VK_A){

                pressL = false; 

            }

            if(e.getKeyCode() == KeyEvent.VK_D){

                pressR = false;


            }

            if(e.getKeyCode() == KeyEvent.VK_W){

                pressU = false;


            }

            if(e.getKeyCode() == KeyEvent.VK_S){

                pressD = false; 


            }
        }   

        public void keyTyped(KeyEvent e){

        }
    });

球员的动作:

public void playerMovement(){ 
   player.horizontalMovement(0);
   player.verticalMovement(0)

   map.horizontalMovement(0);   
   map.verticalMovement(0);     

   if(pressR && !pressL && !pressU && !pressD){
         if(!east){
         toggleRight();                 
         }

         if(collision("east"))
         east = true;                               
     }          

   if(pressL && !pressR && !pressD && !pressU){
        if(!west)
        toggleLeft();                    

        if(collision("west"))
        west = true;
   }

   if(pressD && !pressU && !pressR && !pressL){
        if(!south)
        toggleDown();

        if(collision("south"))
        south = true;
   }

   if(pressU && !pressD && !pressL && !pressR){ 
        if(!north)
        toggleUp();

        if(collision("north"))
        north = true;

   }
}

下面是碰撞测试的位置:

public boolean collision(String loc){

    Rectangle pR = player.getBound();
    Rectangle pM = map.getBound(0, 0);

    if(loc.equals("east")){     
        if(pR.equals(pM)){
            if(west)                
            return false;               

            if(!west)               
            return true;                
        } west = false; south = false;north = false;                            
     }

    if(loc.equals("west"))      
            if(pR.intersects(pM)){
                if(east)                
            return false;   

            if(!east)               
            return true;        
        } east = false; south = false;north = false;
    }

    if(loc.equals("south")){
        if(pR.intersects(pM)){
                if(north)               
            return false;


            if(!north)              
            return true;    


        } north = false; west = false;east = false; 
    }

    if(loc.equals("north")){
        if(pR.intersects(pM)){
                if(south)               
            return false;


            if(!south)              
            return true;                        

        } south = false; west = false;east = false; 
    }

    return false;
}

我将代码设置为这样,以避免在与正在测试的块碰撞时被卡住。它可以工作,但我遇到了很多错误。一个例子是,有时我会被卡住,或者玩家可以通过水平键按下垂直键来通过区块。我很难找到合适的算法。顺便说一句,方向是基于观众的方向,而不是球员的方向

有人能告诉我一个方法吗?谢谢


共 (1) 个答案

  1. # 1 楼答案

    对于播放器和墙(块),创建Shapes(例如可以使用Polygon)。使用Shape's方法

    public boolean intersects(double x, double y, double w, double h);
    public boolean intersects(Rectangle2D r);
    

    或者您可以从Shapes创建Areas并使用

    public void intersect(Area rhs)