Jump to content

View more

Image of the Day

Working on an auto spawn system. #gamedev #indiedev #screenshotsaturday https://t.co/Mm2kfekz7b
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

How to make my ship fire a laser?

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
7 replies to this topic

#1 Zeus_   Members   

122
Like
Likes
Like

Posted 19 November 2001 - 04:22 PM

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

#2 Fruny   Moderators   

1658
Like
Likes
Like

Posted 20 November 2001 - 04:04 AM

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.

#3 Anonymous Poster_Anonymous Poster_*   Guests   

Likes

Posted 20 November 2001 - 04:19 AM

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


#4 Zeus_   Members   

122
Like
Likes
Like

Posted 20 November 2001 - 02:34 PM

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...

#5 Beer Hunter   Members   

712
Like
Likes
Like

Posted 20 November 2001 - 06:03 PM

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.

#6 Vlion   Members   

151
Like
Likes
Like

Posted 21 November 2001 - 05:23 PM

Cool.

I came, I saw, I got programmers block.
~V''''lion

#7 hpox   Members   

122
Like
Likes
Like

Posted 21 November 2001 - 05:31 PM

The stuff after the colon is called an initializing list.

#8 AdmiralBinary   Banned   

100
Like
Likes
Like

Posted 21 November 2001 - 05:53 PM

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.




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.