当物体在彼此的顶部产生时重新定位
如标题中所述,我正在编写一个代码,其中对象彼此产卵。有没有办法防止这种情况?这是我的代码:
// Check if the enemy UFO leaves the screen
if (enemyUfoPos[i].Y > screenHeight)
{
// Update UFOs position
enemyUfoPos[i].Y = -200;
enemyUfoRecs[i].X = rng.Next(1, 1720);
// Decrease the players score
playerScores[SHOOTER] += ufoPass;
}
// Assume UFOs collide with eachother
bool isColliding = true;
while (isColliding == true)
{
enemyUfoRecs[i].X = rng.Next(1, 1720);
isColliding = false;
for (int j = 0; j <= enemyUfoRecs.Length - 1; j++)
{
if (i != j && enemyUfoRecs[j].Intersects(enemyUfoRecs[i]))
{
isColliding = true;
}
break;
}
}
如果有人可以给我一些建议,这将不胜感激。
As stated in the title, I am writing a code where objects are spawning on top of each other. Is there a way to prevent this? Here is my code:
// Check if the enemy UFO leaves the screen
if (enemyUfoPos[i].Y > screenHeight)
{
// Update UFOs position
enemyUfoPos[i].Y = -200;
enemyUfoRecs[i].X = rng.Next(1, 1720);
// Decrease the players score
playerScores[SHOOTER] += ufoPass;
}
// Assume UFOs collide with eachother
bool isColliding = true;
while (isColliding == true)
{
enemyUfoRecs[i].X = rng.Next(1, 1720);
isColliding = false;
for (int j = 0; j <= enemyUfoRecs.Length - 1; j++)
{
if (i != j && enemyUfoRecs[j].Intersects(enemyUfoRecs[i]))
{
isColliding = true;
}
break;
}
}
If anyone could give me some advice that would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是您要归类的逻辑:
区别在do-while循环中,并在do-nile循环时至少遍历代码,然后查看
wher
> 之后检查是否需要再次重复。I think this is the logic you want to archieve:
The difference is in the do-while loop, with a do-while loop, it goes through the code at least once, and then look at the
while
check afterwards if it needs to be repeated again.