Jump to content
  • Advertisement
Sign in to follow this  
nano511

Problem with shooting bullets

This topic is 2529 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Right now when i try to shoot the monsters dont get hit
Here is the code to see if the monster got hit



bool Monster::IsHit( int bulletXPos, int bulletYPos )
{
if( bulletXPos >= xPos + 10 && bulletXPos <= xPos + 30 )
{
if( bulletYPos >= yPos && bulletYPos <= yPos + 74 )
{
health -= 1;
xVel = 0;
return true;
}
}

return false;
}




And here is some other relevant code that is preformed before monster.IsHit() is called


// If the player presses space, create a new bullet
if( player->shoot == true )
{
bullet = new Bullet;
bullets.push_back( bullet );
bullets.front()->xPos = player->xPos;
bullets.front()->yPos = player->handgun.yPos;
if( player->direction == LEFT )
bullets.front()->xVel = -15;
else if( player->direction == RIGHT )
bullets.front()->xVel = 15; }

// Move the bullet
if( !(bullets.empty()) )
{
bullets.front()->xPos += bullets.front()->xVel;
}


E: i also get this error sometimes:

Debug Assertion Failed!
2010\...\include\vector
Line: 70

Expression: vector iterator not dereferencable

For information on how your program can cause assertion failure, see the Visual C++ documentation on assets

Share this post


Link to post
Share on other sites
Advertisement
Most likely it's incorrect math, but I don't know what the dimensions of your 'Monster' sprite is, so I'm not entirely sure on that. But do the bullets act as you want them to? It's just that there is no collision detection ?

Share this post


Link to post
Share on other sites

Most likely it's incorrect math, but I don't know what the dimensions of your 'Monster' sprite is, so I'm not entirely sure on that. But do the bullets act as you want them to? It's just that there is no collision detection ?


The dimensions are fine. There are no 'actual' bullet's atm so i cant really see whats going on but from debugging im pretty sure they're acting as i want them to.. should i make a bullet sprite to see?

Share this post


Link to post
Share on other sites
Ok so what happens if a monster is hit? Is the bullet removed so that you can shoot a new bullet? I don't know if one bullet is enough to kill a monster but if it isn't and the bullet is never removed it will not be much to see. If one bullet isn't enough to kill a monster it will not be much to see. The bullet will go through the monsters and move outside the screen.

Share this post


Link to post
Share on other sites
Video doesn't exist, but I think you should make a sprite for the bullet, it will aid in the debugging of your game, and also provide a new milestone in your game(New art) :]
But yeah, make a sprite, and see what happens to the bullet...

You'll have to add some code to determine whether or not to draw the bullet sprite, like for example
[font="CourierNew, monospace"] [/font]

[source lang='cpp']
[font="CourierNew, monospace"]if(!monster.isHit(bulletx, bullety))[/font]
[font="CourierNew, monospace"]{[/font]
[font="CourierNew, monospace"] apply_surface(blah...);[/font]
[font="CourierNew, monospace"] or for allegro[/font]
[font="CourierNew, monospace"] masked_blit(blah..);[/font]
[font="CourierNew, monospace"]}[/font]
[/source]

[font="CourierNew, monospace"]But basically this just checks to see if the function isHit(int,int) returns true[/font]
[font="CourierNew, monospace"]and if it returns false, then it would draw the bullet as usually but if the function returns true[/font]
[font="CourierNew, monospace"]it would never execute the code within' the 'if' statement.[/font]
[font="CourierNew, monospace"]As for collisions, I recommend adding 'int' variables, called x1, y1, x2, y2, x3, y3, x4, y4[/font]
[font="CourierNew, monospace"]to your 'Monster' class, to provide more OO code. x1, would be top-left corner X and y1 would be top-left corner Y.[/font]
[font="CourierNew, monospace"]Etc, as for initializing these variables, they would need to be constantly updated, in the while loop that your game runs on, so just stick function in there, to update the bounding-box coordinates continuously, some of this might not be optimized code, but for the complexity of your game, I don't think you'll need 'optimized' code.[/font]
[font="CourierNew, monospace"] [/font]
[font="CourierNew, monospace"]EDIT....[/font]
[font="CourierNew, monospace"]P.S I'd make the bullet sprite a circle, within' a 16x16 square...Try and make the bullet any other way, and you'll soon discover why I recommended you do circles...[/font] Edited by lukeymoo

Share this post


Link to post
Share on other sites
Oooh sorry i forgot i was messing around with my albums lol. here is the video


The bullet is actually a rectangle. I was thinking the same thing about aiding in debugging, so i made the sprite before i made the video so you could see. In the video the bullet doesnt go too far if nothing hit it, but i fixed it.

PS. I dont get why i should have made it a circle?

I fixed it. The problem was i was using .front() instead of .back()

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!