Public Group

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

## 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 on other sites
woops double 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 ?

##### 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 on other sites
You are aware that you actually only move one bullet?

##### Share on other sites
Yeah i only want one bullet in the game at a time

##### 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 on other sites
Nah man it takes five shots to kill a monster. When a monster is hit the bullet is removed

##### 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"]}[/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 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()

1. 1
Rutin
21
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 13
• 38
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631715
• Total Posts
3001868
×