Public Group

# Why is only one monster appearing at a time?

This topic is 2536 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?

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631781
• Total Posts
3002316
×