2013-04-21 107 views
0

我試圖讓玩家無法從左側和右側通過平臺,並讓玩家停在平臺之上。到目前爲止,我所有的碰撞事件都是左右兩邊,但右側不起作用,剩下的就是竊聽。我無法弄清楚如何做一個平臺遊戲的基本碰撞,任何人都可以給我一個工作解決方案?Java平臺遊戲衝突

Player類代碼:

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Rectangle; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 


public class Player implements KeyListener 
{ 
int x, y, width, height; 

boolean jump, left, right; 

PlayerThread playerThread; 

int maxHeight = 40; 
double heightC = 0; 
boolean onPlatform = false; 
boolean landed = true; 
int prevY; 
int prevX; 
Rectangle playRect = new Rectangle(); 
boolean leftCollision = false; 
boolean rightCollision = false; 
Platform p; 

public Player() 
{ 
    x = 500; 
    y = 350; 
    width = 40; 
    height = 50; 
    playerThread = new PlayerThread(); 
    playerThread.start(); 
} 

public void paint(Graphics g) 
{ 
    String str = String.valueOf(x); 
    String str2 = String.valueOf(y); 

    g.setColor(Color.RED); 
    playRect.setBounds(x, y, width, height); 
    g.fillRect(x, y, width, height); 

    g.drawString("X: " + str + ", Y: " + str2, 100, 100); 
} 

public void update(Platform p) 
{ 
    this.p = p; 
    CheckForCollision(p); 

} 

public void CheckForCollision(Platform p) 
{ 
    int pX = p.getX(); 
    int pY = p.getY(); 
    int pWidth = p.getWidth(); 
    int pHeight = p.getHeight(); 

    //COLLISION WITH PLATFORM CODE 
    if (playRect.intersects(p.plat) && left == true && !jump && !right && landed) 
    { 
     System.out.println("LEFT"); 
     x = prevX; 
     leftCollision = true; 
    } 
    else 
    { 
     leftCollision = false; 
    } 

    if (playRect.intersects(p.plat) && right == true && !jump && !right && landed) 
    { 
     System.out.println("RIGHT"); 
     x = prevX; 
     rightCollision = true; 
    } 
    else 
    { 
     rightCollision = false; 
    } 
} 

public class PlayerThread extends Thread implements Runnable 
{ 
    public void run() 
    { 
     while (true) 
     { 
      if (left && !leftCollision) 
      { 

       prevX = x; 
       x -= 2; 

      } 

      if (right && !rightCollision) 
      { 
       prevX = x; 
       x += 2; 
      } 

      if (jump) 
      { 
       if (heightC >= maxHeight) 
       { 
        System.out.println(heightC); 
        jump = false; 
        heightC = 0; 
       } 
       else 
       { 
        heightC += 1.5; 
        prevY = y; 
        y -= 5; 
        landed = false; 
       } 
      } 

      //GRAVITY CODE 
      if (!jump && !landed) 
      { 
       if (y < 400 - height) 
       { 
        prevY = y; 
        y += 5; 
        //landed = false; 
       } 
      } 

      if (y >= 400 - height && !landed) 
      { 
       y = 400 - height; 
       landed = true; 
      } 

      try 
      { 
       sleep(17); 
      } 
      catch (InterruptedException e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

@Override 
public void keyPressed(KeyEvent e) 
{ 
    switch (e.getKeyCode()) 
    { 
    case KeyEvent.VK_LEFT: 

     left = true; 
     break; 
    case KeyEvent.VK_RIGHT: 
     right = true; 
     break; 
    case KeyEvent.VK_SPACE: 
     if (landed) 
     { 
     jump = true; 
     } 
     break; 
    } 
} 

@Override 
public void keyReleased(KeyEvent e) 
{ 
    switch (e.getKeyCode()) 
    { 
    case KeyEvent.VK_LEFT: 
     left = false; 
     break; 
    case KeyEvent.VK_RIGHT: 
     right = false; 
     break; 
    case KeyEvent.VK_SPACE: 
     break; 
    } 
} 



public int getX() 
{ 
    return x; 
} 

public void setX(int x) 
{ 
    this.x = x; 
} 

public int getY() 
{ 
    return y; 
} 

public void setY(int y) 
{ 
    this.y = y; 
} 

public int getWidth() 
{ 
    return width; 
} 

public void setWidth(int width) 
{ 
    this.width = width; 
} 

public int getHeight() 
{ 
    return height; 
} 

public void setHeight(int height) 
{ 
    this.height = height; 
} 

@Override 
public void keyTyped(KeyEvent e) 
{ 
    // TODO Auto-generated method stub 

} 
} 
+0

嘗試源看出[這裏]( http://stackoverflow.com/a/14575043/418556)如果你不能從中找出解決方案,請發佈你的最佳嘗試[SSCCE](http://sscce.org/)。 – 2013-04-21 14:09:31

+0

@AndrewThompson我已經創建了超過4個線程已經在這個論壇上,我已經嘗試了所有可能的解決方案,但它總是或者不好或者做我想做的事情的相反。 – 2013-04-21 14:17:51

+0

我鏈接的來源(這是一個SSCCE順便說一句)在這裏堅如磐石。 ***贊成,已經創建了一個SSCCE!***如果沒有,我投票結束爲「不是真正的問題」。 – 2013-04-21 14:20:35

回答

0

這是什麼,我掙扎了很久,我知道有一些對this.Obviously解決方案的需要,不管以何種方重置你的球員位置碰撞發生在平臺上。

所以我做的是檢查矩形是否先相交。然後我嘗試弄清楚它碰撞的平臺的哪一側。我通過在玩家矩形每邊的外側創建一個矩形來實現這一點。然後我得到平臺矩形與每個矩形的交點。然後我計算交叉點的面積。哪一側具有最大交叉面積就是發生碰撞的一側。然後,您可以根據與他碰撞的平臺側面重新設置玩家位置。我創建了一個Mask類,使它非常易於使用my game engine

複雜的部分看起來像這樣。 掩模是類別I來表示的矩形

此方法返回布爾值的數組中的一個是真是側時發生的碰撞上

public static int left=2,top=3,right=0,bottom=1; 
public boolean[] collisionSide(Mask m){ 

    boolean[] leftUp = new boolean[4]; 

    Rectangle[] boxes = new Rectangle[4]; 
    boxes[0] = new Rectangle(this.getRect().x-1, this.getRect().y, 1, this.getRect().height); 
    boxes[1] = new Rectangle(this.getRect().x, this.getRect().y-1, this.getRect().width, 1); 
    boxes[2] = new Rectangle(this.getRect().x + this.getRect().width, this.getRect().y, 1, this.getRect().height); 
    boxes[3] = new Rectangle(this.getRect().x, this.getRect().y+this.getRect().height, this.getRect().width, 1); 

    double greatestArea = 0; 
    int greatest = 0; 

    for(int bbb = 0; bbb<4; bbb++){ 
     if(Calc.getArea(boxes[bbb].createIntersection(m.getRect())) > greatestArea){ 
      greatestArea = Calc.getArea(boxes[bbb].createIntersection(m.getRect())); 
      greatest = bbb; 
     } 
    } 

    for(int b=0; b<4; b++) 
     leftUp[b] = false; 

    leftUp[greatest] = true; 

    return leftUp; 
}