我是XNA和CSharp编程的新手,所以我想学习一个寻宝游戏作为开始,所以我做了一个可以向上,向下,向左和向右走的玩家(作为一个类) . 我还制作了一个Gem类,玩家可以与之碰撞,宝石消失并播放声音 . 但是我想制作一些玩家可以碰撞并停下来的墙,所以我创建了一个名为Tile.cs(墙类)的类,我在其中做了一个空白
public void CollideCheck(bool tWalk, bool bottomWalk, bool leftWalk, bool rightWalk, Rectangle topRect, Rectangle bottomRect, Rectangle rightRect, Rectangle leftRect)
{
colRect = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
if (this.colRect.Intersects(topRect))
{
tWalk = false;
}
else
tWalk = true;
if (this.colRect.Intersects(bottomRect))
{
bottomWalk = false;
}
else
bottomWalk = true;
if (this.colRect.Intersects(leftRect))
{
leftWalk = false;
}
else
leftWalk = true;
if (this.colRect.Intersects(rightRect))
{
rightWalk = false;
}
else
rightWalk = true;
}
然后,在Game1.cs(主类)中,我创建了一个“Tiles”数组:
Tile[] tiles = new Tile[5];
在更新无效我做了这个:
foreach (Tile tile in tiles)
{
tile.CollideCheck(player.topWalk, player.bottomWalk, player.leftWalk, player.rightWalk,
new Rectangle((int)player.Position.X, (int)player.Position.Y - (int)player.Speed.Y, player.currentAnim.FrameWidth, player.currentAnim.FrameHeight),
new Rectangle((int)player.Position.X, (int)player.Position.Y + (int)player.Speed.Y, player.currentAnim.FrameWidth, player.currentAnim.FrameHeight),
new Rectangle((int)player.Position.X + (int)player.Speed.X, (int)player.Position.Y, player.currentAnim.FrameWidth, player.currentAnim.FrameHeight),
new Rectangle((int)player.Position.X - (int)player.Speed.X, (int)player.Position.Y, player.currentAnim.FrameWidth, player.currentAnim.FrameHeight));
}
所有这些矩形都是玩家的边界,但是当我运行游戏时,玩家不会与它发生碰撞,那么有什么方法可以解决这个问题吗?
如果我不是很清楚,我可以发布项目 .
1 回答
您的参数仅在,但您在调用中设置了它们的值 . 您必须将它们声明为
out
变量,以便将它们的值发送回调用方 . 使用out
还可确保在退出函数之前始终为它们设置值 .因此,将函数声明更改为
public void CollideCheck(out bool tWalk, out bool bottomWalk, out bool leftWalk, out bool rightWalk, Rectangle topRect, Rectangle bottomRect, Rectangle rightRect, Rectangle leftRect)
,然后返回值 .