• 13
• 15
• 27
• 9
• 9

# Boolean Toggle

This topic is 2141 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am making a simple space shooter but Im having a problem dealing with my bullets. Its set up to have a list of bullets and it resuses the bullets once they reach the end of the screen or collide with an enemy. Once they do either of these things they are set to Invisible (visible = false). The problem is this only happens the first time the bullet is used. Once it gets reused its set to visible again. Except once it collides it wont go invisible. Im not sure is it something Im missing but I have done checks with Console writes to make sure the methods are calling which they are. But this one line is not working. Any help is appreciated.

 if (Collide(alien, laser)) { alien.setPos(random.Next(50, 650), 0); laser.Visible = false; Console.Write("hit"); score++; }

##### Share on other sites
Hi!

Can you give us a little more code, e.g. the reinitialization of the laser, the declaration of the method of the code piece you showed us, the definition of the laser class/struct or any other spot where the visible-flag is touched?

It might be anything between "laser is a struct and was passed by value to the function doing the logic you showed us” to “semantic error in the reinitialization of a bullet”.

Cheers!

##### Share on other sites
 foreach (Laser laser in bullets) { if (laser.Visible == true) { foreach (Alien alien in enemies.getEnemies()) { if (Collide(alien, laser)) { alien.setPos(random.Next(50, 650), 0); laser.Visible = false; Console.Write("hit"); score = score + (1 * mul); } } } }
 public void shootLaser() { bool newBullet = true; foreach(Laser laser in bullets) { if(laser.Visible == false) { Console.Write("old"); laser.startPos = new Vector2(playerPos.X + 25, playerPos.Y); laser.pos = new Vector2(playerPos.X + 25, playerPos.Y); laser.Visible = true; newBullet = false; } } if(newBullet) { Console.Write("new"); bullets.Add(new Laser(bullet, (playerPos.X + 25), playerPos.Y, true)); } } 

Those are the two places where it is affected (as far as I can see). The first is where it checks for collision and goes "invisibile". The second is where it shoots and should be come visible again. The way it seems to work is the newest one in the list works correctly. It goes visible and invisible at the correct times. But all the older ones wont go invisible after collision.

##### Share on other sites
Hm, I think there is a break missing, isn’t it?

 public void shootLaser() { bool newBullet = true; foreach(Laser laser in bullets) { if(laser.Visible == false) { Console.Write("old"); laser.startPos = new Vector2(playerPos.X + 25, playerPos.Y); laser.pos = new Vector2(playerPos.X + 25, playerPos.Y); laser.Visible = true; newBullet = false; // ------------------------------------- break; // ------------------------------------- } } if(newBullet) { Console.Write("new"); bullets.Add(new Laser(bullet, (playerPos.X + 25), playerPos.Y, true)); } } 
I guess you only want one bullet to become visible, when you shoot.

Maybe it gets easier, if you maintain two lists: one for the bullets alive and one for the dead bullets (as some sort of object pool). This way you’d also do the collision checks only with alive bullets.

Hope this helps.