I’m trying to detect collision on a list of enemy objects, however it’s only working on the last created object. I’ve tried both :
for (int i = 0; i < enemies.Count; i++)
{
if (IntersectsPixel(player.rectangle, player.textureData, enemies[i].rectangle, enemies[i].textureData))
{
touched = true;
}
else
{
touched = false;
}
}
And :
foreach (Enemy enemy in enemies)
{
if (IntersectsPixel(player.rectangle, player.textureData, enemy.rectangle, enemy.textureData))
{
touched = true;
}
else
{
touched = false;
}
}
Both of which I’m putting in the Update method. The enemies are created every few seconds so i know the detection works, but once the next one appears, the last one stops working…
you check last item only, you must break loop, when
IntersectsPixeltrue and process the objector use linq to collect all thus objects: