2013-11-09 89 views
0

我的第一個等距遊戲有問題。我不知道該如何對我的球員靠近牆邊。在這一刻,玩家可能會在綠色區域移動。第一個等距遊戲

我的地圖:

int[,] map = new int[,] 
     { 

      {1, 1, 1, 1, 1, 1, 1, 1}, 
      {1, 0, 0, 0, 0, 0, 0, 1}, 
      {1, 0, 0, 0, 0, 0, 0, 1}, 
      {1, 0, 0, 0, 0, 0, 0, 1}, 
      {1, 0, 0, 0, 0, 0, 0, 1}, 
      {1, 0, 0, 0, 0, 0, 0, 1}, 
      {1, 0, 0, 0, 0, 0, 0, 1}, 
      {1, 1, 1, 1, 1, 1, 1, 1} 

     }; 

變量:

int TileWidth = 50; 
int TileHeight = 50; 
int posX = 2; // map X position 
int posY = 2; // map Y position 
float playerX = 2 * 50; // player X position 
float playerY = 2 * 50; // player Y position 

檢測牆:

public bool detectSolidTile(int x, int y) 
    { 

     if (map[y, x] == 1) return true; else return false; 

    } 

Movemet:

posX = (int)(Math.Floor((playerX)/50)); 
posY = (int)(Math.Floor(playerY/50)); 

(...) 

     if (slide == 1 && !detectSolidTile(posX + 1, posY)) 
     { 
      playerX++; 
     } 
     if (slide == 2 && !detectSolidTile(posX - 1, posY)) 
     { 
      playerX--; 
     } 

圖片 - >http://s16.postimg.org/cxkfomemd/tiles.jpg

我需要什麼改進,以便能夠從牆上搬到牆上?

最好的問候,Krzysiek

回答

0

嘗試使所有的地圖,您可以瀏覽0,然後將地圖,您可以當試圖作出新的舉措檢查不是1 如果未來地位將是A 1或0.如果它是0,你讓他移動,否則你阻止他。

if (slide == 1 && !detectSolidTile(posX + 1, posY)) 
    { 
     playerX++; 
    } 

這將檢測到1作爲可移動區域外的地圖,從而阻止它去你想要的地方。

你明白現在的問題在哪裏?

另一種解決方案是理解矩陣的大小,並儘快x或y來訪問,你停止增加他們的最大值。但是,如果您稍後想要添加障礙,請繼續保持現在正在進行的操作,但請確保0代表地圖,1代表地圖外部。

所以這裏是一個BartoszKP編輯: 你也許是正確的,這個「!」沒有解決他的代碼,我只是解釋他應該怎麼做。

我將用於他的問題的代碼略有不同。

所有的首次下降PlayerX的和playerY,因爲它是完全一樣的東西POSX和波西,你只是做額外的計算。 現在這是說你的運動算法會是這個樣子:

變量:

int TileWidth = 50; 
int TileHeight = 50; 
int posX = 2; // map X position - same thing as playerX 
int posY = 2; // map Y position - same thing as playerY 
//posX and posY will now hold the position of your player on the map since your conversion from playerX to playerY will only work if you increment a value by 50 or more. 

檢測牆上: //如果在座標x和y的瓷磚是一堵牆我返回true //否則返回false 公共BOOL detectSolidTile(INT X,int y)對 {

if (map[y, x] == 1) return true; 
    else return false; 
} 

機芯:

posX = (int)(Math.Floor((playerX)/50)); //drop this you don't need it 
posY = (int)(Math.Floor(playerY/50)); //drop this too, you are using posX and posY to store locations 

(...) 

     if (slide == 1 && !detectSolidTile(posX + 1, posY)) 
     { 
      posX++; 
     } 
     if (slide == 2 && !detectSolidTile(posX - 1, posY)) 
     { 
      posX--; 
     } 

     if (slide == 3 && !detectSolidTile(posX, posY+1)) 
     { 
      posY++; 
     } 
     if (slide == 4 && !detectSolidTile(posX, posY-1)) 
     { 
      posY--; 
     } 

如果您使用posX和posY作爲玩家在地圖上的位置,這應該工作的很好。 爲玩家制作一種類型的座標併爲地圖製作一種類型,這使得它更容易混淆。但是,如果你確實需要使用不同的座標,對他們來說,你應該總是試圖計算出這樣的運動時,請參閱PlayerX的和playerY:

if (slide == 1 && !detectSolidTile((int)(Math.Floor((playerX+1)/50)) + 1, posY)) 
{ 
    playerX++; 
} 

,這是因爲你被1不改變PlayerX的價值posX的值,這會限制你的移動。如果這是令人困惑的讓我知道,我會盡力解釋這一點。

+0

更改1到0幫助,但只有右牆。玩家只有在綠色區域:(移動 IMG http://i.stack.imgur.com/k6xG8.jpg 編輯:我做:DI改變 如果(幻燈片== 2 && detectSolidTile(POSX - 1, posY)) 到 if(slide == 2 && detectSolidTile(posX,posY)) – Krzysiek

+0

對y使用相同的過程可以使它向上和向下移動。 –