2012-12-31 130 views
2

所以我試圖在我的遊戲中實現碰撞檢測,並出於某種原因碰撞無法正常工作。碰撞檢測Java 2D Sprite

public class ArenaKeys extends KeyAdapter { 
    arenaScreenBuild arena; 
    int xPos = 0, playerFace = 4; 
    int xPPos = 200, yPPos = 150; 
    int pX = 40, pY = 30; 
    AttackAshe aAtk = new AttackAshe(); 

    int[][] mask = new int[400][92]; 

    @SuppressWarnings("static-access") 
    public void keyPressed(KeyEvent e) { 
     int keyCode = e.getKeyCode();// Get key pressed 
     if (keyCode == e.VK_RIGHT) { 
      playerFace = 4; 
      xPos += 5; 
      pX = (xPos + xPPos)/5; 
      if (checkBoundary(pX, pY) == (false)) 
       xPos -= 5; 
     } else if (keyCode == e.VK_LEFT) { 
      playerFace = 3; 
      xPos -= 5; 
      pX = (xPos + xPPos)/5; 
      if (checkBoundary(pX, pY) == (false)) 
       xPos += 5; 
     } else if (keyCode == e.VK_UP) { 
      playerFace = 2; 
      yPPos -= 5; 
      pY = yPPos/5; 
      if (checkBoundary(pX, pY) == (false)) 
       yPPos += 5; 
     } else if (keyCode == e.VK_DOWN) { 
      playerFace = 1; 
      yPPos += 5; 
      pY = yPPos/5; 
      if (checkBoundary(pX, pY) == (false)) 
       yPPos -= 5; 
     } 
     if (keyCode == e.VK_SPACE) { 
      aAtk.regArrow(arena.xPosition(), arena.yPosition()); 
      arena.shoot(playerFace); 
      arena.xArrow = xPPos; 
      arena.yArrow = yPPos; 
     } else if (keyCode == e.VK_ESCAPE) 
      System.exit(0); 
     arena.moveArena(xPos); 
     arena.turnPlayer(playerFace); 
     arena.updateScreen(xPPos, yPPos); 
    } 

    public boolean checkBoundary(int x, int y) { 
     Rectangle t1 = new Rectangle(Turret.x, Turret.y, Turret.WIDTH, 
       Turret.HEIGHT); 
     Rectangle p = new Rectangle(pX, pY, Player.WIDTH, Player.HEIGHT); 

     if (t1.intersects(p)) 
      // if (mask[x][y] == 0) 
      return false; 
     else 
      return true; 
    } 
    public static class Turret { 
     static int x = 168; 
     static int y = 40; 
     static final int WIDTH = 50; 
     static final int HEIGHT = 50; 
    } 

    public static class Player { 
     static final int WIDTH = 25; 
     static final int HEIGHT = 25; 
    } 

    public ArenaKeys(arenaScreenBuild arena) throws Exception { 
     this.arena = arena; 
    } 
} 

在實際炮塔前約20個單位,精靈不能再移動。即使你變得非常高或者非常低,精靈也不能走到炮塔的上方或下方。

什麼似乎是錯誤的是,精靈過早碰撞到炮塔矩形,但我不明白它是如何可能的。我把炮塔準確地畫成50寬,在168,40高50。玩家正在移動,所以它的x,y每次都有所不同,但它的尺寸是25寬25高。

My Turret

原來的炮塔是126x111約,但我繪製爲50×50

Sample Sprite 25×25

public class arenaScreenBuild extends JPanel implements ActionListener { 
    String picPath = "pictures/"; 
    String[] fileName = { "stageBridge.png", "turret.png", "Ashe.png", 
      "regArrow.png", "arenaScreen.png" }; 
    ClassLoader cl = arenaScreenBuild.class.getClassLoader(); 
    URL imgURL[] = new URL[5]; 
    Toolkit tk = Toolkit.getDefaultToolkit(); 
    Image imgBG, imgTurret, imgPlayer, imgRegArrow, imgBorder; 
    Boolean[] ptFunc = new Boolean[3]; 

    int PLAYER_INITIAL_X = 200, PLAYER_INITIAL_Y = 150; 
    int xPos = 0, xPFace = 150, yPFace = 0; 
    int xPPos = 200, yPPos = 150; 
    int xVal, yVal, xArrow, yArrow, xTemp, yTemp; 
    Timer space; 
    int counter, facePosition = 1; 

    public arenaScreenBuild() throws Exception { 
     for (int x = 0; x < 5; x++) { 
      imgURL[x] = cl.getResource(picPath + fileName[x]); 
     } 
     imgBG = tk.createImage(imgURL[0]); 
     imgTurret = tk.createImage(imgURL[1]); 
     imgPlayer = tk.createImage(imgURL[2]); 
     imgRegArrow = tk.createImage(imgURL[3]); 
     imgBorder = tk.createImage(imgURL[4]); 
     for (int x = 0; x < 3; x++) 
      ptFunc[x] = true; 

     space = new Timer(100, this); 

    } 

    public void updateScreen() { 
     repaint(); 
    } 

    public void moveArena(int x) { 
     xPos = x; 
    } 

    public void updateScreen(int x, int y) { 
     xPPos = x; 
     yPPos = y; 
     repaint(); 
    } 

    public boolean arrow() { 
     return (true); 
    } 

    public void turnPlayer(int face) { 
     if (face == 4) { 
      xPFace = 150; 
     } else if (face == 3) { 
      xPFace = 100; 
     } else if (face == 2) { 
      xPFace = 50; 
     } else if (face == 1) { 
      xPFace = 0; 
     } 
     if (yPFace == 50) 
      yPFace = 0; 
     else if (yPFace == 0) 
      yPFace = 50; 
    } 

    public void paintComponent(Graphics g) { 
     g.drawImage(imgBG, 10, 30, 610, 490, xPos, 0, xPos + 600, 460, this); 

     g.drawImage(imgTurret, 850 - xPos, 200, 950 - xPos, 300, 0, 0, 126, 
       110, this); 
     g.drawImage(imgTurret, 1350 - xPos, 200, 1450 - xPos, 300, 0, 0, 126, 
       110, this); 

     g.drawImage(imgPlayer, xPPos, yPPos, 50 + (xPPos), 
       50 + (yPPos), xPFace, yPFace, xPFace + 50, yPFace + 50, this); 

     if (counter <= 5000 && counter > 0) 
      g.drawImage(imgRegArrow, xArrow, yArrow, this); 

     g.drawImage(imgBorder, 0, 0, 620, 600, 0, 0, 620, 600, this); 

     g.setColor(Color.WHITE); 
     g.drawString("x:" + (xPPos + xPos), 535, 525); 
     g.drawString("y:" + yPPos, 535, 545); 
    } 

    public int xPosition() { 
     xVal = xPPos + xPos; 
     return (xVal); 
    } 

    public int yPosition() { 
     yVal = yPPos; 
     return (yVal); 
    } 

    public void shoot(int i) { 
     facePosition = i; 
     xTemp = xPosition(); 
     yTemp = yPosition(); 
     space.start(); 
    } 

    public void actionPerformed(ActionEvent e) { 
     counter++; 

     if (facePosition == 4) { 
      if (counter <= 5000) { 
       xArrow += 50; 
      } 
     } 

     else if (facePosition == 3) { 
      if (counter <= 5000) { 
       xArrow -= 50; 
      } 
     } 

     else if (facePosition == 2) { 
      if (counter <= 5000) { 
       yArrow -= 50; 
      } 
     } 

     else if (facePosition == 1) { 
      if (counter <= 5000) { 
       yArrow += 50; 
      } 
     } 

     if (xArrow == (xTemp + 100)) { 
      counter = 0; 
      space.stop(); 
     } 

     updateScreen(); 
    } 
} 
+0

它是什麼做錯了什麼?比「不工作」更具體。 – Zyerah

+0

對不起,我忘了寫,更新了這個問題。 – Exikle

+0

我的第一個建議是嘗試一次硬碰撞檢查,而不是使用內置於Java中的函數。這將從字面上理解sprite的大小,並檢查另一個sprite是否碰到這些邊界並與這些像素重疊。 – Zyerah

回答

2

原來,我對x位置值是錯誤的。同樣由於我以前的邊界代碼,還有它的復原,使我搞砸了,因此不讓我早點看到問題。

對於任何一個尋找如何使碰撞檢測邊界,只要2個矩形和使用

Rectangle rect1 = new Rectangle(topLeftX, topLeftY, rectangleWidth,rectangleHeight); 
Rectangle rect2 = new Rectangle(topLeftX, topLeftY, rectangleWidth, rectangleHeight); 
if (rect1.intersects(rect2)) 
//enter code to do if it intersects here 
+0

對於任何詢問我爲什麼我沒有接受我自己的答案的人,我不能等到2天時間到來 – Exikle

+1

同時考慮執行['Shape']類的'interects()'方法(http:// docs .oracle.com/javase/7/docs/api/java/awt/Shape.html)界面。 – trashgod