for-in-loop/ Condition 仅用于 a List 的第一个元素
我已经搜索了三天,没有找到解决方案,代码如下:
if (keyboardState.IsKeyDown(Keys.Right))
{
for (int i = GlobalClass.BlocksPositions.Count - 1; i > 0; i--)
{
if (new Rectangle((int)GlobalClass.BlocksPositions[i].X, (int)GlobalClass.BlocksPositions[i].Y, bT.Width, bT.Height).Intersects(new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height)))
{
c = 0;
}
else
{
c = 1;
}
}
if (c == 1)
{
Position.X += Speed;
}
}
每个块位置等于我可以通过单击屏幕创建的块,然后将新块位置放入列表中。基本上我的 BlockPosition 列表中有一个块坐标列表。然后我传递每个块位置的条件,条件为每个块位置创建一个矩形,并为玩家创建一个矩形...如果发生碰撞,玩家将不会朝该方向移动。当我尝试代码时,我的角色将仅与列表的第一个元素碰撞,而不与其他元素碰撞,如果我删除第一个元素,它将与下一个元素碰撞,但不会与其他元素碰撞。所有变量都很好我知道这一点,因为我尝试用这样的代码替换此代码:
if (keyboardState.IsKeyDown(Keys.Right))
{
for (int i = GlobalClass.BlocksPositions.Count - 1; i > 0; i--)
{
if (new Rectangle((int)GlobalClass.BlocksPositions[i].X, (int)GlobalClass.BlocksPositions[i].Y, bT.Width, bT.Height).Intersects(new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height)))
{
GlobalClass.BlocksPositions.RemoveAt[i];
}
}
}
同样的事情,但在这里如果它发生冲突,我会删除列表的元素,这是相同的条件,但当我尝试它时,它会检测到所有元素并删除我触摸的元素。我尝试了 foreach 函数,得到了相同的结果。怎么了?我已经用变量做了很多事情,所以我确信问题不是来自它们的值,而是来自我对它们所做的事情。请帮忙! (:
I have searched for three days and didn't find a solution, Here is the code:
if (keyboardState.IsKeyDown(Keys.Right))
{
for (int i = GlobalClass.BlocksPositions.Count - 1; i > 0; i--)
{
if (new Rectangle((int)GlobalClass.BlocksPositions[i].X, (int)GlobalClass.BlocksPositions[i].Y, bT.Width, bT.Height).Intersects(new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height)))
{
c = 0;
}
else
{
c = 1;
}
}
if (c == 1)
{
Position.X += Speed;
}
}
Each Block position equals a block that I can create by clicking on the screen, the new block position is then put in the List. Basically I have a list of blocks Coordinates in my BlockPosition List. Then I pass the condition for each blockposition, the Condition Create A rectangle for each BlockPosition and one for the Player... if there's a collision, the player won't move in that direction. When I try the code, My character will Collide only with the first element of the List and not the others, if I delete the first element it will then collide with the next one but not the others. All the variables are FINE I know it because I tried to replace this code by something like this:
if (keyboardState.IsKeyDown(Keys.Right))
{
for (int i = GlobalClass.BlocksPositions.Count - 1; i > 0; i--)
{
if (new Rectangle((int)GlobalClass.BlocksPositions[i].X, (int)GlobalClass.BlocksPositions[i].Y, bT.Width, bT.Height).Intersects(new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height)))
{
GlobalClass.BlocksPositions.RemoveAt[i];
}
}
}
Same thing but here if it collides I delete the Element of the List, it's the same condition but when I try it it will detect all of the elements and delete the ones that I touch. I tried the foreach function and I get the same Results. What's wrong? I already do a lot of things with does variables So I'm sure the problem don't come from their values but with what I do with them. Help please! (:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发现我必须像这样打破循环:
Found out I have to break the loop like this: