use*_*668 0 c# xna collision-detection
继续我在这里的游戏(一个游戏,玩家是一艘船,你击落流星).我目前正在研究射击子弹的船只,并且我试图在被子弹击中时移除流星.所以这就是我所做的.我几乎在那里,但我在编码中发现了一个错误.在我的游戏中,它产生了地图右侧以外的流星,它们向左移动,然后点击它们,如果你以正确的顺序拍摄流星而不是其他方式,这是有效的.让我用图片解释一下.

如果我击落第二颗流星,标有数字1的流星将首先被摧毁
//spawns the enemies.
public void LoadEnemies()
{
int randY = random.Next(100, 500);
if (spawn > 1)
{
spawn = 0;
if (enemies.Count() < 4)
enemies.Add(new Enemies(Content.Load<Texture2D>("meteor"), new Vector2(1110, randY)));
}
//Here's where the error lies because of the bulletcolliding (I think)
for (int i = 0; i < enemies.Count; i++)
{
if (!enemies[i].isVisible || bulletColliding)
{
bulletColliding = false;
enemies.RemoveAt(i);
i--;
}
}
}
Run Code Online (Sandbox Code Playgroud)
碰撞方法.
public void bulletCollision(GameTime gameTime)
{
foreach (var x in bullets)
{
foreach (var y in enemies)
{
enemy_rect = new Rectangle((int)y.position.X, (int)y.position.Y, 10, 10);
bullet_rect = new Rectangle((int)x.position.X, (int)x.position.Y, 10, 10);
if (bullet_rect.Intersects(enemy_rect))
{
bulletColliding = true;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
基本上,我对如何移除被击中的特定流星毫无头绪,我需要你的帮助.我得到了所有帮助.
小智 5
不要只使用bool bulletColliding.使用整数表示应该销毁的敌人数量,并在每个敌人类别中保存他的号码信息.
第二个选项可以是保持一个bool ifDestroy最初为假的敌人类,如果敌人被摧毁则将其改为真.你可以检查一下loadEnemies().