How to make my ship fire a laser?

Started by
6 comments, last by Zeus_ 22 years, 4 months ago
I have a ship class, and every time the user hits the ''p'' key, the ship should emit a laser. I have code like this: player::fire(){ glPushMatrix(); glColor3f(0.0, 0.0, 1.0); glLineWidth(3); glBegin(GL_LINES); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 1.0, 0.0); glEnd(); for(float a=0.0; a<10.0; a+=0.05){ glTranslatef(0.0, a, 0.0);} glPopMatrix(); } But it doesn''t work... I want it so that whenever I hit the fire button, a little line appears, and translates to the top of the screen (as in out of site) along the y axis.. I cant seem to get this to work. Please help! ~Jesse
Advertisement
You have to do the translation before the drawing. Plus, if you want an animation, you will have to do it over several frames. You cannot just put it into a single for loop.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
You need a shot class, such as:

class Shot
{
float x, y, z;

public:
Shot(float X, float Y, float Z): x(X), y(Y), z(Z) {}

void Update() {
glPushMatrix();
glColor3f(0.0, 0.0, 1.0);
glLineWidth(3);
glTranslatef(x, y, z);

glBegin(GL_LINES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glEnd();

y += .05;
}

bool Done() {
return y == 10;
}
};

Then you can make a new Shot whenever your ship fires (keep a
list of the activve ones) then call Update on each frame and
remove them when Done == true

- Loki
Uhh...

I saw that colon in there, where you put

Shot(float X, float Y, float Z): x(X), y(Y), z(Z) {}

...

I have never seen a colon used like that.. Honestly.

In 5 years of programming, I have never seen that. And it still compiled/executed perfectly.

Could you tell me what that means?!! And maybe the syntax? Whats it used for!?

Thanks.

I fell really stupid...
It does look wierd... that''s not your fault

It''s purpose is simply to assign values to class members. It''s like saying:

x = X; y = Y; z = Z;

It''s generally done the "wierd" way, though. I''m not sure of the reason it''s done that way, but I''m sure someone can explain.
Cool.

I came, I saw, I got programmers block.
~V''''lion
~V'lionBugle4d
The stuff after the colon is called an initializing list.
...
An initialization list calls the constructors of the members listed there with the parameters u pass to them. For instance:

class Number
{
public:
Number(int newval) { val = newval; }
int val;
};

class Word
{
public:
Word(string newval) { val = newval; }
string val;
};

class Pair
{
public:
Pair():a(5),b("Hello!") {}
Number a;
Word b;
};

Sometimes useful for those who dote on OOOP (Over-Object-Oriented-Programming).

---------------

I finally got it all together...
...and then forgot where I put it.

This topic is closed to new replies.

Advertisement