Sign in to follow this  

Boolean Toggle

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

If you intended to correct an error in the post then please contact us.

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.


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

Share this post


Link to post
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”. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Cheers!

Share this post


Link to post
Share on other sites
[CODE]
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);
}
}
}
}[/CODE]
[CODE]


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));
}

}
[/CODE]

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 this post


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

[CODE]
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));
}
}
[/CODE]
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. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this