#include "bullet.h"
Bullet *bullet = NULL; // Pointer to Bullet class instance
void inputKeyPressed()
{
.
.
.
// Check if fire button is pressed
if (keys[VK_SPACE])
{
bullet = new Bullet; // Instance of Bullet class
bullet->buildBulletList(); // Create display list for the bullet
bullet_sin_angle = (GLfloat)sin (ship->angle * 3.1415/180);
bullet_cos_angle = (GLfloat)cos (ship->angle * 3.1415/180);
bullet->x_pos_1 = ship->x_pos;
bullet->y_pos_1 = ship->y_pos;
bullet->fired = true;
bullet->angle_1 = ship->angle; // Set angle that bullet will travel
}
// Check if bullet fired and is within left/right screen boundaries
// If so, alter x position of bullet
if (bullet != NULL && /*bullet->fired &&*/
bullet->x_pos_1 > screen_left &&
bullet->x_pos_1 < screen_right)
{
bullet->x_pos_1 += bullet->x_vel_1 * -bullet_sin_angle;
bullet->y_pos_1 += bullet->y_vel_1 * bullet_cos_angle;
}
} // End of method inputKeyPressed
How to create new objects in-game?
I''m writing an Asteroids-type game and am having trouble with firing bullets. At the moment, I have a bullet created during initialisation and it just sits in the middle of the screen until the spacebar is pressed, when it fires of in the direction the spaceship is facing. What I want to do is actually create a new bullet (I have a Bullet class) when the spacebar is pressed and fire it off. I have attempted this and the relevant parts of the code look like this:
The code compiles and links but causes an error when run:
"Meng project has caused an error in MENG PROJECT.EXE.
Meng project will now close." I think it is to do with that last IF statement. Can anyone help please?
Write something like this:
if (bullet != NULL)
if(bullet->x_pos_1 > screen_left &&
bullet->x_pos_1 < screen_right)
{ bullet->x_pos_1 += bullet->x_vel_1 * -bullet_sin_angle;
bullet->y_pos_1 += bullet-vel_1*bullet_cos_angle;
}
It should work but i''m still not sure
if (bullet != NULL)
if(bullet->x_pos_1 > screen_left &&
bullet->x_pos_1 < screen_right)
{ bullet->x_pos_1 += bullet->x_vel_1 * -bullet_sin_angle;
bullet->y_pos_1 += bullet-vel_1*bullet_cos_angle;
}
It should work but i''m still not sure
(It looks as if) you are multiply new:ing to the same pointer which is BAD and you don''t seem to put anything into x_vel_1 and y_vel_1 at all.
Use a std::vector or an array to hold the bullets and remember to delete them.
Use a std::vector or an array to hold the bullets and remember to delete them.
I could be totally off here... What is your constructor like for the Bullet class? If I was initializing an object I would have:
bullet = new Bullet(xPos, yPos); or something rather than
bullet = new Bullet;
Just a thought.
bullet = new Bullet(xPos, yPos); or something rather than
bullet = new Bullet;
Just a thought.
Hi,
I don''t know if this will help, but your "if(bullet!=NULL...)" is pretty useless. If your bulletpointer should prove to be NULL your code will already have written to invalid memorylocations (bullet->x_pos_1) and accessed nonexisting memberfunctions (bullet->buildBulletList().
As I said, don''t know if it will help, but your should do something about it.
Best of luck to you
Kotumo
I don''t know if this will help, but your "if(bullet!=NULL...)" is pretty useless. If your bulletpointer should prove to be NULL your code will already have written to invalid memorylocations (bullet->x_pos_1) and accessed nonexisting memberfunctions (bullet->buildBulletList().
As I said, don''t know if it will help, but your should do something about it.
Best of luck to you
Kotumo
Kotumo: If the bullet != NULL fails, then c++ will short circuit out of the if statement, and will not test the other conditions. It will go only as far as it needs to to fail the conditional.
Billy: I don''t see anything wrong with the section of code you have shown us. Does the crash occur when you press space, or start the program? Otherwise We cannot realy fathom how the problem could occur. I can only offer guidelines: Ensure that you test for bullet != NULL everywhere in your code befor you reference it; the same goes for the ship and asteroids; when you delete the bullet, reset the pointer to null; I repeat, reset the pointer to null. Otherwise debug, or output I''m here, now I''m here statements so you can track the problem down.
Billy: I don''t see anything wrong with the section of code you have shown us. Does the crash occur when you press space, or start the program? Otherwise We cannot realy fathom how the problem could occur. I can only offer guidelines: Ensure that you test for bullet != NULL everywhere in your code befor you reference it; the same goes for the ship and asteroids; when you delete the bullet, reset the pointer to null; I repeat, reset the pointer to null. Otherwise debug, or output I''m here, now I''m here statements so you can track the problem down.
Gleno:
What I ment was instead of:
it should be
What I ment was instead of:
bullet = new Bullet;bullet->buildBulletList();
it should be
bullet = new Bullet;if(bullet!=NULL) {bullet->buildBulletList();}
I see in your comment that you create a new display list for the bullet each time it is created. I do not know what you do in that section of the code, but I imagine it is relevant. First try to create the display list once outside of the bullet first then pass it to the bullet, that way you only need to compile the list once, and remeber to tell OpenGL to delete it later.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement