# Why is only one monster appearing at a time?

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

## Recommended Posts

every time there are no monsters left in my monster vector, i call this function:
 void Game::NewRound() { round += 1; if( player->health < 10 ) player->health++; player->points += 250; for( int i = 0; i < round; i++ ) { monster = new Monster( &GameEnvironment ); monsters.push_back( monster ); } } 

shouldnt the for loop create more then one monster? And i know the problem isnt that the other monsters arent being drawn because the function is only called when the vector is empty, and for the vector to be empty all the monsters have to be dead.

##### Share on other sites

every time there are no monsters left in my monster vector, i call this function:
 void Game::NewRound() { round += 1; if( player->health < 10 ) player->health++; player->points += 250; for( int i = 0; i < round; i++ ) { monster = new Monster( &GameEnvironment ); monsters.push_back( monster ); } } 

shouldnt the for loop create more then one monster? And i know the problem isnt that the other monsters arent being drawn because the function is only called when the vector is empty, and for the vector to be empty all the monsters have to be dead.

the problem is not in the code you posted, its elsewhere.

##### Share on other sites
Without seeing the line of code causing the problem, I would guess the cause is round. Set a breakpoint on the [color=#1C2837][font=CourierNew, monospace][size=2][color=#000000]round [color=#666600]+=[color=#000000] [color=#006666]1[color=#666600];[/font]line and what is it's value as it enters the function? My guess is 0.

If that fails, set a breakpoint just before exit of that function and whats the length of monsters?

##### Share on other sites

And i know the problem isnt that the other monsters arent being drawn because the function is only called when the vector is empty, and for the vector to be empty all the monsters have to be dead.

My guess is that there are more than one monsters being created; it's just they all share the same coordinates. Either the Monster's coordinates are hard-coded or you're seeding the random number generator every time you call rand (a common beginner mistake -- srand should only be called once during program execution).

##### Share on other sites
I seed the number generator in the monster's constructor.

The problem is that they have the same coordinates because i checked the size at the end of the function and the size is as it is suppose to be.

When i try putting 'srand( time( 0 ) );' in main it says "this declaration has no storage class or type specifier". I tried including ctime and time.h but still says the same thing

##### Share on other sites

I seed the number generator in the monster's constructor.

Check what is being included here, not sure which is needed for srand, perhaps stdlib.h
http://www.cplusplus.com/reference/clibrary/cstdlib/srand/

##### Share on other sites
for std::srand you include <cstdlib> and for std::time you include <ctime>

##### Share on other sites
yeah i seed the random number generator thing at the beginning of int main(). thanks guys. Its not working yet but i cant wait to see it working lol

E: By its not working yet i emant other problems lol. They ARE spawning in different locations now. Which is why i thanked you

##### Share on other sites

yeah i seed the random number generator thing at the beginning of int main(). thanks guys. Its not working yet but i cant wait to see it working lol

If you looking at per unit random value seeding I would do something like srand(Time(NULL) + MyVector.size() );

I think time only returns seconds so its possible that you creating all of your things within the same second.
So your seed ends up being the same for each one.

##### Share on other sites
If you looking at per unit random value seeding I would do something like srand(Time(NULL) + MyVector.size() );

And what advantages does that have compared to calling srand(time(0)) in main?

##### Share on other sites
THANKY YOU GUYS SO MUCH!!! Theres more then one monster now! But the way i implemented shooting causes a problem. I tried to make it as simplistic as i could, so pretty much did this
 if( monster is on the left side of the player and player is looking left ) { if( player presses space ) { zombie is hurt; } } 

The problem is, now with multiple zombies ALL of them on my left get hurt if im looking left and press space. So, what is the usual approach to 2D gun games concerning shooting?

##### Share on other sites

So, what is the usual approach to 2D gun games concerning shooting?

This usually entails some sort of array of bullets, to which you create new bullets as the player presses the space bar. Then you add logic to check for intersections from the monster array and the bullet array:

 for each bullet in bullets for each monster in monsters if intersects( monster, bullet ) then if monster.damage( amount ) < 0 then remove monster from monsters remove bullet from bullets 

##### Share on other sites

[quote name='yewbie' timestamp='1313168959' post='4848322']If you looking at per unit random value seeding I would do something like srand(Time(NULL) + MyVector.size() );

And what advantages does that have compared to calling srand(time(0)) in main?
[/quote]
None, but I figured there would be someone wanting to do this for some reason /shrug