Why is only one monster appearing at a time?

Started by
11 comments, last by yewbie 12 years, 8 months ago
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.
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.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
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?

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

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/

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

for std::srand you include <cstdlib> and for std::time you include <ctime>
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

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

This topic is closed to new replies.

Advertisement