Can't break out of for loop?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
TheThirdL3g
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 14
Joined: Wed Apr 15, 2009 6:40 pm

Can't break out of for loop?

Post 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
Last edited by TheThirdL3g on Fri Jan 14, 2011 1:43 pm, edited 1 time in total.
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: Can't break out of for loop?

Post by N64vSNES »

Timer?
TheThirdL3g
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 14
Joined: Wed Apr 15, 2009 6:40 pm

Re: Can't break out of for loop?

Post by TheThirdL3g »

N64vSNES wrote:Timer?
Elaborate?....please :)
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: Can't break out of for loop?

Post 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 )
User avatar
thejahooli
Chaos Rift Junior
Chaos Rift Junior
Posts: 265
Joined: Fri Feb 20, 2009 7:45 pm
Location: London, England

Re: Can't break out of for loop?

Post 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.
I'll make your software hardware.
TheThirdL3g
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 14
Joined: Wed Apr 15, 2009 6:40 pm

Re: Can't break out of for loop?

Post by TheThirdL3g »

Thanks for the help.

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