2013-05-29 20 views
2

對不起,我不知道標題是否是解釋它的最好方法。我正在使用libgdx在java中製作pacman克隆。我已經完成了地圖的渲染以及瓷磚的碰撞檢測。唯一的問題是,無論如何,pacman都會繼續移動。當他向右走,並且你按下或者被阻擋時,他停下來。我嘗試了一堆不同的東西,似乎無法讓它正常工作。Pacman克隆,當一個按鈕被按下時,我怎麼能讓pacman移動到難以訪問的方向

這是我吃豆子機類

package com.psillidev.pacman1; 

import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; 
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; 
import com.badlogic.gdx.math.Rectangle; 

public class player { 
    private static int height = 16; 
    private static int width = 16; 

    private int playerX , playerY; 
    private int direction; 
    private Texture playerTexture; 
    private boolean isAlive; 


    public player(int startX,int startY) { 
     playerTexture = new Texture(Gdx.files.internal("data/pacman.png")); 
     playerX = startX; 
     playerY = startY; 
    } 

    public void move(Map map) { 
     if (direction == 0) { 
      if (isNoTile(map,direction,getX(),getY())) { 
       setY(getY() + (int)(Gdx.graphics.getDeltaTime() + 2)); 
      } 
     } 

     if (direction ==1) { 
      if (isNoTile(map,direction,getX(),getY())) { 
       setX(getX() + (int)(Gdx.graphics.getDeltaTime() + 2)); 
      } 
     } 

     if (direction ==2) { 
      if (isNoTile(map,direction,getX(),getY())) { 
       setY(getY() - (int)(Gdx.graphics.getDeltaTime() + 2)); 
      } 
     } 

     if (direction ==3) { 
      if (isNoTile(map,direction,getX(),getY())) { 
       setX(getX() - (int)(Gdx.graphics.getDeltaTime() + 2)); 
      } 
     } 

    } 

    private boolean isNoTile(Map map, int dir, int translated_x, int translated_y) { 
     System.out.println("X: " + translated_x + " Y: " + translated_y); 
     for (Rectangle r : map.getBlockedTiles()) { 

      switch (dir) { 
      case 0: 
       Rectangle up = new Rectangle(translated_x,translated_y + 2 , 16, 16); 

       if (up.overlaps(r)) { 

        return false; 

       } 
       break; 

      case 1: 
       Rectangle right = new Rectangle(translated_x+2,translated_y, 16, 16); 
       if (right.overlaps(r)) { 
        return false; 
       } 
       break; 

      case 2: 
       Rectangle down = new Rectangle(translated_x,translated_y - 2, 16, 16); 
       if (down.overlaps(r)) { 
        return false; 
       } 
       break; 

      case 3: 
       Rectangle left = new Rectangle(translated_x-2,translated_y, 16, 16); 
       if (left.overlaps(r)) { 
        return false; 
       } 
       break; 

      default: 
       break; 
      } 
     } 

     return true; 
    } 

    public void render(SpriteBatch batch) { 
     batch.draw(playerTexture, playerX, playerY); 
    } 

    public int getX() { 
     return playerX; 
    } 

    public void setX(int x) { 
     playerX = x; 
    } 

    public int getY() { 
     return playerY; 
    } 

    public void setY(int y) { 
     playerY = y; 
    } 

    public int getDirection() { 
     return direction; 
    } 

    public void setDirection(int dir) { 

     direction = dir; 
    } 

    public Rectangle getRect() { 
     return new Rectangle(playerX,playerY,width,height); 
    } 
} 
+0

你必須處理方向的時候吃豆子來了其他的片。否則它會繼續移動。 – wisemann

回答

1

我無法從你提供的代碼回答,但我的建議是,你需要做一個調整,你KeyListener類。我假設你有一個KeyListener正在聽用戶按下箭頭鍵。當按下某個鍵時,你是否在做類似Pacman.setDirection()的事情?

你真正需要做的是在調用setDirection()之前檢測方向是否有效。所以你的KeyListener的代碼將是這樣的......

  1. 確定用戶是否按下向上,向下,向左或向右
  2. 檢查遊戲網格/地圖上看到,如果方向是有效的基於Pacman的當前位置
  3. 如果方向有效,請致電setDirection()。如果沒有,不要打電話給任何人,並且Pacman會繼續按照目前的方向。
0

這真的不應該由玩家本身來處理,但也許由運動經理來處理,使得遊戲更容易測試/擴展。在你的代碼中,你只允許他按照你的輸入決定的方向移動瓷磚,所以如果你向右走時向上按UP,那麼他自動停止移動,如果UP是牆,不管右邊是什麼。

你可以做的是輸入:如果要求向上移動,檢查是否爲空,然後向上移動,如果不是空的,則繼續向前移動的方向移動。其他方向也一樣。

0

你setDirection方法不正確,它需要

public void setDirection(int dir) { 
    // only change direction if we can. 
    if (isValidDirection(dir)){ 
     direction = dir; 
    } 
} 
相關問題