Jump to content
  • Advertisement
Sign in to follow this  
nano511

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.

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

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

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


Thats probably your problem.

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 this post


Link to post
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 :P

Share this post


Link to post
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 this post


Link to post
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 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!