ClioSport.net

Register a free account today to become a member!
Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

  • When you purchase through links on our site, we may earn an affiliate commission. Read more here.

Anyone a programmer (c++, although quite standard question)



Right long story short. Im making a game and have created a bullet class. It only fires one bullet so ofc I have to make it an array. I have tried the following and it doesn't work -

bullet class:
class Bullet {

private:

bool alive;
double x, y;
double s;
double ang;

public:
Bullet(){
}

void Bullet::Update() {

if(alive) {

x += s * cos(ang);
y += s * sin(ang);
}
}

void Bullet::Fire(double startX, double startY, double angle, double speed) {

if (alive == false) {
x = startX;
y = startY;
ang = angle;
s = speed;
alive = true;
}
}

void Bullet::Draw() {

if (alive) {

bulletSourceRect.top = 0;
bulletSourceRect.bottom = 25;
bulletSourceRect.left = 75;
bulletSourceRect.right = 100;

bulletDestRect.top = (long)y;
bulletDestRect.bottom = bulletDestRect.top + 15;
bulletDestRect.left = (long)x;
bulletDestRect.right = bulletDestRect.left + 15;

pTheDrawEngine->Blit(bulletDestRect, bulletSourceRect, pBullet);
}
}

};

And in main -

Bullet bullets[20];

if (pTheInputs -> IfMouseNewLeftDown() ) {
bullets.Fire(132, player.getYPosition() + 32, player.getAngle(), 5);
}
bullets.Update();
bullets.Draw();
}


Yet it is still only firing one bullet :S. The IfMouseNewLeftDown() only is true if the previous sample of the mouse is false meaning only one bullet is fired at a time and not all fired at once
 

sn00p

ClioSport Club Member
  A blue one.
Without seeing the full code it's hard to read what's going on, but you've defined at static array called bullets and then accessed it like:


Bullets.fire(...)


C transparently handles pointers like this, so although bullets is an array, you haven't accessed it like one, so it defauts to only accessing the first object, so you've really written:


Bullets[0].fire(...)


If you want to fire all 20 bullets you'll have to do a loop.

for(I=0;I (less than) 20;I++)
Bullets (open square bracket) I (close square bracket).fire(...)

(sorry on tapatalk and I can't put in the less than brackets as it stops anything else appearing after)
 

sn00p

ClioSport Club Member
  A blue one.
Without seeing the full code it's hard to read what's going on, but you've defined at static array called bullets and then accessed it like:


Bullets.fire(...)


C transparently handles pointers like this, so although bullets is an array, you haven't accessed it like one, so it defauts to only accessing the first object, so you've really written:


Bullets[0].fire(...)


If you want to fire all 20 bullets you'll have to do a loop.

for(I=0;I (less than) 20;I++)
Bullets (open square bracket) I (close square bracket).fire(...)

(sorry on tapatalk and I can't put in the less than brackets as it stops anything else appearing after)

On a computer now..

Array accesses are done like Bullets.fire(...)

Bullets.fire(...) is equivalent to Bullets.fire(...)

Hence why you only see a single bullet, it doesn't call fire for every object in the array.
 
Argh sorry man, I haven't included the for loop in the main code I have posted. That IS in there and I am still getting that problem.

I don't think it is a problem with creating the objects (I tried 2 different user inputs, left mouse and right mouse) and I managed to fire 2 bullets then.
 
Last edited:

sn00p

ClioSport Club Member
  A blue one.
BatFastard said:
Argh sorry man, I haven't included the for loop in the main code I have posted. That IS in there and I am still getting that problem.

I don't think it is a problem with creating the objects (I tried 2 different user inputs, left mouse and right mouse) and I managed to fire 2 bullets then.

The code you've posted for firing though isn't correct and will only ever use the first object in the array, regardless of whether there is a loop or not.

Without seeing the original code, it's hard to diagnose what you've done.
 

sn00p

ClioSport Club Member
  A blue one.
Humn.

Now I'm not using tapatalk I can see more code and the brackets which are missing in tapatalk.

The code looks ok from a cursory glance, you'd need to post more to see what the problem is.

It will jump out at you soon!

Edit: what's the netleftmousedown function? Sounds to me
like a function that will only return true if the mouse is a new click since you last called it. If it does do that? Then that is most likely your problemif you're holding the button down.
 
The code you've posted for firing though isn't correct and will only ever use the first object in the array, regardless of whether there is a loop or not.

Without seeing the original code, it's hard to diagnose what you've done.

oo ok I understand what your saying now. Not got an e-mail by any chance I could send you a copy to? Been at it now since last night and this morning and starting to completely do my nut in.
 


Top