2012-04-06 56 views
-1

我遇到了牆碰撞問題。基本上我希望我的球員每當碰到一個街區時就停下來。 這是我做了這麼遠:2D地圖上的塊碰撞問題平鋪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; 
} 

我成立我的代碼喜歡這個,以避免當我與我正在測試的塊碰撞時被卡住。它的工作原理,但我遇到了很多錯誤。一個例子是有時候我被卡住了,或者玩家可以通過用水平鍵按壓垂直方向的方塊。我有問題找出適當的算法。順便說一句,方向是基於觀衆的方向而不是玩家的方向。

有人可以跟我分享一下血統嗎?謝謝。

+0

儘量在你的問題更具體,更側重於具體的問題。 – ahanin 2012-04-06 06:53:00

+0

嗨,我的問題是,我設置的代碼沒有做我想做的事情。例如,當玩家正確地向右碰撞並碰撞到一個障礙物時,碰撞會執行,但是當我按下W時,玩家就會卡住,我必須按S才能上升,反之亦然。我希望玩家檢測到指定瓷磚的每一面都是封閉的瓷磚,因此玩家無法在各個方向上穿過它。我正在尋找一個可以告訴我它是如何完成的下降代碼。 – krazaz 2012-04-06 08:03:22

回答

1

對於玩家和牆壁(塊)創建Shapes(例如可以使用Polygon)。使用Shape's方法

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

或者你可以創建一個從ShapesAreas和使用

public void intersect(Area rhs) 
+0

@StanuslavL,你能給我一個關於如何做到這一點的示例代碼?我是新來的Java。謝謝! – krazaz 2012-04-06 08:08:25

+0

@krazaz不使用KeyListener使用KeyBindings – mKorbel 2012-04-06 08:13:25