我正在努力与我的玩家或敌人的碰撞矩形和水平/墙壁之间的碰撞。SdlDotNet平台游戏:碰撞矩形A(玩家)碰撞矩形B(等级)
该级别是一维数组。
这样很好:屏幕上有一个平台和一个围绕着关卡的盒子,所以玩家和敌人都无法离开舞台。
在我的Game_Manager类(处理游戏,它是更新和碰撞)绘制关卡,玩家和一个敌人。效果很好,但玩家和敌人可以离开该地区,因为碰撞检测是不正确的:
在我Game_Manager类玩家侦察到敌人,它的位置是复位,当他们碰撞:
if(player.PlayerCollRect.IntersectsWith(enemy.enemyCollRect))
{
player.playerPosition = playerStartPosition;
}
再次:运作良好。
但:我似乎无法得到与1D阵列(我的水平)的碰撞权利。 而且我似乎遇到了在我的关卡中获得每个块/瓦片的碰撞矩形的问题 - 我的玩家可以出去,我的敌人也可以出去。
敌人是粉红色,玩家绿色。 红块,不使用现在: See here what my level looks like and whats happening (.gif)
我已经试过:
player.PlayerCollRect.IntersectsWith(enemy.EnemyCollRect)
我应该尝试与我的瓷砖碰撞?或者我的完整水平?如何?
我对Level.cs
public abstract class Level
{
protected byte[,] byteArray;
public Tile[,] tileArray;
public Rectangle levelTileColl;
public Level()
{
CreateTileArray();
tileArray = new Tile[byteArray.GetLength(0), byteArray.GetLength(1)];
CreateWorld();
}
protected abstract void CreateTileArray();
private void CreateWorld()
{
for (int r = 0; r < byteArray.GetLength(0); r++)
{
for (int c = 0; c < byteArray.GetLength(1); c++)
{
if(byteArray[r, c] == 1)
{
tileArray[r, c] = new Tile(new Point(c * 40, r * 40));
//tileArray[r, c] = new Tile(new Point(c * tile.TileWidth, r * tile.TileHeight));
}
}
}
}
public void Draw(Surface showTiles)
{
for (int r = 0; r < byteArray.GetLength(0); r++)
{
for (int c = 0; c < byteArray.GetLength(1); c++)
{
if (tileArray[r, c] != null)
tileArray[r, c].Draw(showTiles);
}
}
}
}
代码Level01.cs代码:
public class Level01 : Level
{
protected override void CreateTileArray()
{
byteArray = new Byte[,]
{
{ 1, 1, 1, 1, 1, 1, 1, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
};
}
}
代码Tile.cs:
public class Tile
{
private Surface tileImage;
private Point tilePosition;
public Rectangle tileColl;
private int tileWidth = 40;
private int tileHeight = 40;
public int TileWidth
{
get { return tileWidth; }
set { tileWidth = value; }
}
public int TileHeight
{
get { return tileHeight; }
}
public Rectangle TileColl
{
get { return tileColl; }
set { tileColl = value; }
}
public Tile(Point position)
{
tileImage = new Surface("tile.png");
tilePosition = position;
tileColl = new Rectangle(tilePosition.X, tilePosition.Y, tileWidth, tileHeight);
}
public void Draw(Surface showTiles)
{
showTiles.Blit(tileImage, tilePosition);
}
}
- 编辑 - 好的,让我以不同的方式说明: 如何检查我的玩家在我的关卡中是否触摸/碰撞/与tile瓦片相交? 我有一个tileArray,我想检查碰撞是否有任何碰撞,如果有碰撞,就是1(见上)。
这不符合我的要求。这个想法看起来不错,我看到了逻辑,但奇怪的是我不想要的检测碰撞: 当我跳跃时,我可以在没有任何位置重置的情况下穿过任何平台。 当我稍后跳起并进入一个空的空间(没有平台,没有碰撞)时,碰撞被检测到并且我的球员的位置被重置。 –