c# - Only last member of a list gives a Rectangle Intersect xna -


this similar problem posted here: only last made member of list updating

again have list of players (for multiplayer purposes) , list of blocks(basically texture assigned position , rectangle).

i have function named collision engine supposed detect collisions between each block in list , player.

here function using attempt detect if player intersecting any block (in case 500 randomly generated trees).

foreach (blocks b in main.initializer.blocklist) {     foreach (player p in main.initializer.playerlist)     {                  if (p.hitbox.intersects(b.box))                 {                     p.intersection = true;                 }                 else                 {                     p.intersection = false;                 }      } } 

at first thought wasn't detecting collision @ all, noticed is detecting collision, detects on last placed block in list (figured out after limiting amount of trees 1, 2).

if explain me fix appreciated.

i have secondary option of moving intersect detection switch statement in blocks class if necessary, fix it?

you're overwriting p.intersection in every iteration of loop. don't set false in loop. set false default , change true if collision detected.

edit: need put player in outer loop , blocks in inner one.

     foreach (player p in main.initializer.playerlist)      {         p.intersection = false;          foreach (blocks b in main.initializer.blocklist)         {             if (p.hitbox.intersects(b.box))                 {                 p.intersection = true;                 break;                   }         } } 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -