2014-03-04 84 views
0

我正在做一个小方2d滚动游戏,由于某种原因我的碰撞检测不起作用。我无法弄清楚,我一直试图修复它几天,但似乎没有任何工作。你可以帮我吗!提前致谢!!!!碰撞检测不在2D工作android

public class GameScreen extends Screen { 
enum GameState { 
    Ready, Running, Paused, GameOver 
} 

static GameState state = GameState.Ready; 
// Variable Setup 
// You would create game objects here. 
public Paint paint, paint2; 
public static int gameHeight = AndroidGame.height; 
public static int gameWidth = AndroidGame.width; 
public static int byX = SampleGame.x; 
public static int byY = SampleGame.y; 
public static int x = 60; 
public static int y = AndroidGame.height - 80; 
public static int surface = AndroidGame.height - 80; 
public static int numberSq = 25; 
public static int[] values; 
public double counter2 = 4; 
public int counterStep; 
public Image person; 
public static Image[] walk = new Image[9]; 
public static Image object; 
private Animation anim; 
public static int up = 0; 
public static int right = 0; 
public long jumpTime = 200; 
public static Rect[] valuesSq; 
public static Rect[] valuesSq2; 
public static Rect[] valuesSq3; 
public static Rect rectOne; 
public static Rect rectTwo; 
public int colide = 0; 
public static boolean boolResult; 
private Collision collision; 

public GameScreen(Game game) { 
    super(game); 

    rectOne = new Rect(x, y, 27, 75); 
    valuesSq = new Rect[numberSq]; 
    valuesSq2 = new Rect[numberSq]; 
    valuesSq3 = new Rect[numberSq]; 

    values = new int[numberSq]; 
    values[0] = 300; 
    for (int i = 1; i < numberSq; i++) { 
     values[i] = (values[i - 1] + 200); 

    } 

    object = Assets.object; 
    walk[1] = Assets.walk[1]; 
    walk[2] = Assets.walk[2]; 
    walk[3] = Assets.walk[3]; 
    walk[4] = Assets.walk[4]; 
    walk[5] = Assets.walk[5]; 
    walk[6] = Assets.walk[6]; 
    walk[7] = Assets.walk[7]; 
    walk[8] = Assets.walk[8]; 

    anim = new Animation(); 
    anim.addFrame(walk[1], 50); 
    anim.addFrame(walk[2], 50); 
    anim.addFrame(walk[3], 50); 
    anim.addFrame(walk[4], 50); 
    anim.addFrame(walk[5], 50); 
    anim.addFrame(walk[6], 50); 
    anim.addFrame(walk[7], 50); 
    anim.addFrame(walk[8], 50); 
    person = anim.getImage(); 

    paint = new Paint(); 
    paint.setTextSize(30); 
    paint.setTextAlign(Paint.Align.CENTER); 
    paint.setAntiAlias(true); 
    paint.setColor(Color.WHITE); 

    paint2 = new Paint(); 
    paint2.setTextSize(30); 
    paint2.setTextAlign(Paint.Align.CENTER); 
    paint2.setAntiAlias(true); 
    paint2.setColor(Color.WHITE); 

} 

@Override 
public void update(float deltaTime) { 
    List<TouchEvent> touchEvents = game.getInput().getTouchEvents(); 



    if (state == GameState.Ready) 
     updateReady(touchEvents); 
    if (state == GameState.Running) 
     updateRunning(touchEvents, deltaTime); 
    if (state == GameState.Paused) 
     updatePaused(touchEvents); 
    if (state == GameState.GameOver) 
     updateGameOver(touchEvents); 
} 

private void updateReady(List<TouchEvent> touchEvents) { 

    // This example starts with a "Ready" screen. 
    // When the user touches the screen, the game begins. 
    // state now becomes GameState.Running. 
    // Now the updateRunning() method will be called! 

    if (touchEvents.size() > 0) { 
     state = GameState.Running; 
    } 
} 

private void updateRunning(List<TouchEvent> touchEvents, float deltaTime) { 
    // This is identical to the update() method from our Unit 2/3 game. 
    // x++; 

    right = 1; 
    if (right == 1) { 

     for (int i = 0; i < numberSq; i++) { 
      values[i] -= 4; 
     } 
    } 

    // 1. All touch input is handled here: 
    int len = touchEvents.size(); 
    for (int i = 0; i < len; i++) { 
     TouchEvent event = touchEvents.get(i); 

     if (event.type == TouchEvent.TOUCH_DOWN) { 

      if (event.x < 640) { 
       // Move left. 
      } 

      else if (event.x > 640) { 
       // Move right. 
      } 

     } 

     if (event.type == TouchEvent.TOUCH_UP && y == surface) { 
      up = 1; 
      new Thread(new thread()).start(); 

     } 

     else if (event.x > 640) { 
      // Stop moving right. } 

     } 
    } 
    if (y < surface) { 
     y += 6; 
    } 
    if (y > surface) { 
     y = surface; 
    } 
    if (up == 1) { 
     // Stop moving left. 
     counter2 = 4; 
     counter2 += .1; 
     y = y + (int) ((Math.sin(counter2) + Math.cos(counter2)) * 14.2); 
     if (counter2 >= 10) { 
      counter2 = 4; 
     } 
    } 

    person = anim.getImage(); 

    animate(); 

} 

private void updatePaused(List<TouchEvent> touchEvents) { 
    int len = touchEvents.size(); 
    for (int i = 0; i < len; i++) { 
     TouchEvent event = touchEvents.get(i); 
     if (event.type == TouchEvent.TOUCH_UP) { 

     } 
    } 
} 

private void updateGameOver(List<TouchEvent> touchEvents) { 

    int len = touchEvents.size(); 
    for (int i = 0; i < len; i++) { 
     TouchEvent event = touchEvents.get(i); 
     if (event.type == TouchEvent.TOUCH_UP) { 
      if (event.x > 300 && event.x < 980 && event.y > 100 
        && event.y < 500) { 
       nullify(); 
       game.setScreen(new MainMenuScreen(game)); 
       return; 
      } 
     } 
    } 

} 

@Override 
public void paint(float deltaTime) { 
    Graphics g = game.getGraphics(); 
    // g.drawImage(Assets.background, 0, 0); 
    for (int i = 0; i < numberSq; i++) { 
     valuesSq[0 + i] = new Rect(values[0 + i] - 6, surface - 35, 1, 
       110); 
     valuesSq2[0 + i] = new Rect(values[0 + i] - 6, 0, 1, 110); 
     if (Rect.intersects(rectOne,valuesSq[0 + i]) 
       || Rect.intersects(rectOne,valuesSq2[0 + i])) { 
      state=GameState.GameOver; 
        } else { 
       } 
      } 
    g.drawRect(0, 0, gameWidth, gameHeight, Color.CYAN); 
    g.drawRect(x, y, 27, 75, Color.BLACK); 
    g.drawImage(person, x, y); 
    for (int i = 0; i < numberSq; i++) { 
     g.drawImage(object, values[0 + i] - 6, surface - 35); 
     g.drawImage(object, values[0 + i] - 6, 0); 
    } 
    g.drawString("" + boolResult, 320, 480, paint); 

    // Secondly, draw the UI above the game elements. 
    if (state == GameState.Ready) 
     drawReadyUI(); 
    if (state == GameState.Running) 
     drawRunningUI(); 
    if (state == GameState.Paused) 
     drawPausedUI(); 
    if (state == GameState.GameOver) 
     drawGameOverUI(); 

} 

private void nullify() { 

    paint = null; 

    System.gc(); 
} 

private void drawReadyUI() { 
    Graphics g = game.getGraphics(); 

    g.drawARGB(155, 0, 0, 0); 
    g.drawString("Tap each side of the screen to move in that direction.", 
      320, 480, paint); 

} 

private void drawRunningUI() { 
    Graphics g = game.getGraphics(); 


} 

private void drawPausedUI() { 
    Graphics g = game.getGraphics(); 
    g.drawARGB(155, 0, 0, 0); 
    g.drawString("Resume", 400, 165, paint2); 
    g.drawString("Menu", 400, 360, paint2); 

} 

private void drawGameOverUI() { 
    Graphics g = game.getGraphics(); 
    g.drawRect(0, 0, 320, 480, Color.BLACK); 
    g.drawString("GAME OVER.", 50, 50, paint); 

} 

@Override 
public void pause() { 
    if (state == GameState.Running) 
     state = GameState.Paused; 

} 

@Override 
public void resume() { 

} 

@Override 
public void dispose() { 

} 

@Override 
public void backButton() { 
    pause(); 
} 

public void animate() { 
    anim.update(10); 
} 

public class thread implements Runnable { 

    @Override 
    public void run() { 
     try { 
      Thread.sleep(jumpTime); 
      up = 0; 
     } catch (Exception e) { 
      e.printStackTrace(); 
      new Thread(this).start(); 
      System.exit(0); 
     } 
    } 

} 

} 
+0

快速浏览一下,但我没有看到你在哪里处理任何与任何碰撞......你甚至不使用你的“碰撞”对象......你期望发生什么? – Tonithy

+0

什么?我正在使用矩形,以查看它们是否发生了将状态更改为游戏结束的冲突。 – user3183590

+0

啊。在paint方法中,您要检查rectOne是否与您生成的那些方块相交。 – Tonithy

回答

1

Rect的参数不Rect(x, y, width, height),他们Rect(left, top, right, bottom),所以你的目的,你可能需要使用Rect(x, y, x + width, y - height)