Page 1 of 1

Can't break out of for loop?

Posted: Fri Jan 14, 2011 11:48 am
by TheThirdL3g
For some reason, hopefully a simple mistake, all my bullets are shooting at once. I really don't see anything wrong so I figured I would come here.

Heres the bullet coding. (yet to put in any velocity or frame rate smoothing)

Code: Select all

#define BULLETS 5

class Bullet {
	public:
		Bullet();
		void Update();
		void Fire(float, float);
		void Draw();
		bool alive;
	private:
		float radius;
		float x, y;
		//float xv, yv;
		};
	Bullet bullet[BULLETS];	
		
	
Bullet::Bullet():alive(false), radius(10), x(0), y(0){}

void Bullet::Update() {
	if (alive) {
		y += .1;
		//
		if (y > (windowHeight - 50)) {
			alive = false;
			}
		}
	}
	
	
void Bullet::Fire(float startx, float starty) {
	x = startx;
	y = starty;
	alive = true;
	}
	
void Bullet::Draw() {
	if (alive) {
		glBegin(GL_LINE_LOOP);
		glVertex3f(x-(radius*0.5), y-(radius*0.5), 0.0f);
		glVertex3f(x+(radius*0.5), y-(radius*0.5), 0.0f);
		glVertex3f(x+(radius*0.5), y+(radius*0.5), 0.0f);
		glVertex3f(x-(radius*0.5), y+(radius*0.5), 0.0f);
		
		glEnd();
		
		/*glBegin(GL_LINE_LOOP);
		
		for (int i=0; i < 360; i++)
		{
			float degInRad = i*DEG2RAD;
			glVertex2f(x + cos(degInRad)* (radius*1.5), y + sin(degInRad)*(radius*1.5));
		}
		
		glEnd();*/
		
		}
	}



	if (key[SPACE]) {
		for (int i = 0; i < BULLETS; i++) {
			if (bullet[i].alive == false) {
				bullet[i].Fire(ship.x, ship.y);
				break;
				}
			}
		}



for (int i = 0; i < BULLETS; i++) {
		if (bullet[i].alive) {
			bullet[i].Update();
			bullet[i].Draw();
			}
		}



I've determined it is likely caused by the initial firing since the ship moves when I put this in there:

Code: Select all

if (key[SPACE]) {
		for (int i = 0; i < BULLETS; i++) {
			if (i == 1)  //<<<<<<<<<<<<<<
				ship.x = 300;//<<<<<<<<<<<<<
			if (bullet[i].alive == false) {
				bullet[i].Fire(ship.x, ship.y);
				break; // <<<<<<<<<<<<<<<<<<<<<<<<<<< Is this not working?
				}
			}
		}
I just have no clue why the "break" isn't working or at least seems to not be working.

Any help would be really appreciated

Re: Can't break out of for loop?

Posted: Fri Jan 14, 2011 12:54 pm
by N64vSNES
Timer?

Re: Can't break out of for loop?

Posted: Fri Jan 14, 2011 2:14 pm
by TheThirdL3g
N64vSNES wrote:Timer?
Elaborate?....please :)

Re: Can't break out of for loop?

Posted: Fri Jan 14, 2011 2:19 pm
by N64vSNES
TheThirdL3g wrote:
N64vSNES wrote:Timer?
Elaborate?....please :)
Your probably checking if the space bar is being pressed 60> times a second so therefor they are all getting fired in less than a second.

so

this:

Code: Select all

if (key[SPACE]) {
      for (int i = 0; i < BULLETS; i++) {
         if (i == 1)  //<<<<<<<<<<<<<<
            ship.x = 300;//<<<<<<<<<<<<<
         if (bullet[i].alive == false) {
            bullet[i].Fire(ship.x, ship.y);
            break; // <<<<<<<<<<<<<<<<<<<<<<<<<<< Is this not working?
            }
         }
      }
should be this:

Code: Select all

static Timer = SDL_GetTicks();
if (key[SPACE] && SDL_GetTicks() - Timer > 100) {
      for (int i = 0; i < BULLETS; i++) {
         if (i == 1)  //<<<<<<<<<<<<<<
            ship.x = 300;//<<<<<<<<<<<<<
         if (bullet[i].alive == false) {
            bullet[i].Fire(ship.x, ship.y);
            Timer = SDL_GetTicks();
            break; // <<<<<<<<<<<<<<<<<<<<<<<<<<< Is this not working?
            }
         }
      }
The timer dosen't need to be static since you can store it in the class but that example should work.

EDIT: On further inspection it looks like you might not be using SDL, Allegro perhaps? But yo should find a equivelent with a quick google search. Basically you just need to cap it so it fires at a constant rate ( in my example 100 milliseconds )

Re: Can't break out of for loop?

Posted: Sat Jan 15, 2011 7:04 am
by thejahooli
N64vSNES wrote:EDIT: On further inspection it looks like you might not be using SDL, Allegro perhaps?
I'm pretty sure it's OpenGL, in which case SDL timing would still be used if that's what he's using with his OpenGL.

Re: Can't break out of for loop?

Posted: Sat Jan 15, 2011 7:04 pm
by TheThirdL3g
Thanks for the help.

And yeah I'm using OpenGL with glut... at least till I learn the basics.