【发布时间】:2017-03-01 13:06:46
【问题描述】:
我已经与我的地图发生碰撞,碰撞工作,但是当我的玩家发生碰撞时,我停止了玩家的移动并且他不会走路,因为我不知道如何阻止玩家向一个方向移动
这是我在播放器和地图下的代码冲突
public bool IsCollisionTile(Rectangle player)
{
foreach(Rectangle rect in this._collisionObject)
{
if (rect.Intersects(player))
{
return true;
}
}
return false;
}
玩家不移动的代码在 Game1 类中
if (mapLoader.IsCollisionTile(player.getDestinationRect()))
{
player.setCantWalk(true);
}
是Player.cs类中的update和setCantWalk函数
private bool _cantWalk;
public void Update()
{
this._destinationRectangle.X = (int)_position.X;
this._destinationRectangle.Y = (int)_position.Y;
this._destinationRectangle.Width = _texture.Width;
this._destinationRectangle.Height = _texture.Height;
if(Keyboard.GetState().IsKeyDown(Keys.Up))
{
if (_cantWalk == false)
{
_position.Y--;
}
}
else if (Keyboard.GetState().IsKeyDown(Keys.Down))
{
if (_cantWalk == false)
{
_position.Y++;
}
}
else if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
if (_cantWalk == false)
{
_position.X++;
}
}
else if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
if (_cantWalk == false)
{
_position.X--;
}
}
}
public void setCantWalk(bool walk)
{
_cantWalk = walk;
}
想帮我
【问题讨论】:
-
您可以尝试在 X 轴上移动播放器,进行碰撞检查,然后在 Y 轴上移动并再次进行检查,然后还原导致检查的任何一个...不看起来很理想,但这是一种选择
-
您好,我是初学者,不知道怎么做
标签: c# collision-detection monogame tiled