C# XNA/MonoGame-如何使用矩形作为边界框检查列表中每个对象的碰撞
我正在使用MonoGame框架制作一个平台游戏。该游戏有一些基本的机制,如:重力和碰撞检测,只对列表中的最后一个对象有效C# XNA/MonoGame-如何使用矩形作为边界框检查列表中每个对象的碰撞,c#,xna,monogame,C#,Xna,Monogame,我正在使用MonoGame框架制作一个平台游戏。该游戏有一些基本的机制,如:重力和碰撞检测,只对列表中的最后一个对象有效 if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) Exit(); for (int i = 0; i < boxes.Co
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
for (int i = 0; i < boxes.Count; i++)
{
if (player.Colliding(boxes[i]))
{
player.falling = false;
}
if (!player.Colliding(boxes[i]))
{
player.falling = true;
}
}
player.Update();
boxes.ForEach(k => k.Update());`
以下是游戏区域的代码(主脚本):
`
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
for (int i = 0; i < boxes.Count; i++)
{
if (player.Colliding(boxes[i]))
{
player.falling = false;
}
if (!player.Colliding(boxes[i]))
{
player.falling = true;
}
}
player.Update();
boxes.ForEach(k => k.Update());`
`
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
for (int i = 0; i < boxes.Count; i++)
{
if (player.Colliding(boxes[i]))
{
player.falling = false;
}
if (!player.Colliding(boxes[i]))
{
player.falling = true;
}
}
player.Update();
boxes.ForEach(k => k.Update());`
玩家脚本:
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
for (int i = 0; i < boxes.Count; i++)
{
if (player.Colliding(boxes[i]))
{
player.falling = false;
}
if (!player.Colliding(boxes[i]))
{
player.falling = true;
}
}
player.Update();
boxes.ForEach(k => k.Update());`
`
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
for (int i = 0; i < boxes.Count; i++)
{
if (player.Colliding(boxes[i]))
{
player.falling = false;
}
if (!player.Colliding(boxes[i]))
{
player.falling = true;
}
}
player.Update();
boxes.ForEach(k => k.Update());`
该框仅继承自Object2D.cs
,未更改任何代码
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
for (int i = 0; i < boxes.Count; i++)
{
if (player.Colliding(boxes[i]))
{
player.falling = false;
}
if (!player.Colliding(boxes[i]))
{
player.falling = true;
}
}
player.Update();
boxes.ForEach(k => k.Update());`
但是在MainGame.cs
的更新无效中,我们正在检查碰撞:
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
for (int i = 0; i < boxes.Count; i++)
{
if (player.Colliding(boxes[i]))
{
player.falling = false;
}
if (!player.Colliding(boxes[i]))
{
player.falling = true;
}
}
player.Update();
boxes.ForEach(k => k.Update());`
`
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
for (int i = 0; i < boxes.Count; i++)
{
if (player.Colliding(boxes[i]))
{
player.falling = false;
}
if (!player.Colliding(boxes[i]))
{
player.falling = true;
}
}
player.Update();
boxes.ForEach(k => k.Update());`
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed | | Keyboard.GetState().IsKeyDown(Keys.Escape))
退出();
对于(int i=0;ik.Update())`
代码是有效的,但只适用于列表中的第二个对象,当我移动到添加的第一个框上时,由于重力的作用,我会摔倒
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
for (int i = 0; i < boxes.Count; i++)
{
if (player.Colliding(boxes[i]))
{
player.falling = false;
}
if (!player.Colliding(boxes[i]))
{
player.falling = true;
}
}
player.Update();
boxes.ForEach(k => k.Update());`
我不知道为什么它只检查框列表中的最后一个框。一旦发现冲突,就不会停止搜索。更改此代码:
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
for (int i = 0; i < boxes.Count; i++)
{
if (player.Colliding(boxes[i]))
{
player.falling = false;
}
if (!player.Colliding(boxes[i]))
{
player.falling = true;
}
}
player.Update();
boxes.ForEach(k => k.Update());`
for (int i = 0; i < boxes.Count; i++)
{
if (player.Colliding(boxes[i]))
{
player.falling = false;
}
if (!player.Colliding(boxes[i]))
{
player.falling = true;
}
}
for(int i=0;i
…类似于:
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
for (int i = 0; i < boxes.Count; i++)
{
if (player.Colliding(boxes[i]))
{
player.falling = false;
}
if (!player.Colliding(boxes[i]))
{
player.falling = true;
}
}
player.Update();
boxes.ForEach(k => k.Update());`
var isFalling=true; // assume the worst
for (int i = 0; i < boxes.Count; i++)
{
if (player.Colliding(boxes[i]))
{
isFalling = false;
break;
}
}
player.falling = isFalling;
var isFalling=true;//做最坏的打算
对于(int i=0;i
谢谢!我会试试这个。是的,它只是没有检测到添加的第一个框的冲突,我只是做了一个测试,如果删除了第二个框,它会检测到第一个框的冲突,但是当添加第二个框时,它只检测到第一个框的冲突抱歉,它只检测到第二个框的冲突,当添加第二个框时