2013-11-09 91 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使用相同的过程可以使它向上和向下移动。 –