我有一个有点像我的世界的游戏,但是从自上而下的角度来看,添加块是通过玩家左键单击它在指定位置添加块,右键单击放置的块删除块来完成的。
我遇到的问题是,当玩家左键单击时,它会将块及其位置添加到列表中(以便稍后保存到 XML),因此合乎逻辑的结论是,当玩家右键单击时,它会从游戏中删除所述块,以及它在列表中的位置。
这是我使用的产生滞后的方法:
for (int b = 0; b < game.blocklist.Count; b++)
{
for (int v = 0; v < game.blockpos1.Count; v++)
{
if (game.blocklist[b].visible == true)
{
if (game.cursor.boundingbox.Intersects(game.blocklist[b].blockrectangle) && mousestate.RightButton == ButtonState.Pressed && game.player.Builder == true)
{
if (game.blocklist[b].blockposition.X == game.blockpos1[v].X && game.blocklist[b].blockposition.Y == game.blockpos1[v].Y)
{
game.blockpos1.RemoveAt(v);
game.blocklist.RemoveAt(b);
break;
}
}
}
}
}
现在这是我替换它的方法,它极大地减少了延迟,并且仍然达到了我想要的相同效果:
for (int b = 0; b < game.blocklist.Count; b++)
{
if (game.blocklist[b].visible == true)
{
if (game.cursor.boundingbox.Intersects(game.blocklist[b].blockrectangle) && mousestate.RightButton == ButtonState.Pressed && game.player.Builder == true)
{
if (game.blocklist[b].blockposition.X == game.blockpos1[b].X && game.blocklist[b].blockposition.Y == game.blockpos1[b].Y)
{
game.blockpos1.RemoveAt(b);
game.blocklist.RemoveAt(b);
break;
}
}
}
}
为什么会产生如此多的滞后?我只需要澄清我做错了什么,所以我不会再犯了。