Sign in to follow this  

Why is only one monster appearing at a time?

This topic is 2314 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:
[code]

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 );
}
}
[/code]

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
[quote name='nano511' timestamp='1313165079' post='4848276']
every time there are no monsters left in my monster vector, i call this function:
[code]

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 );
}
}
[/code]

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.
[/quote]

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][color=#666600]+=[/color][color=#000000] [/color][color=#006666]1[/color][color=#666600];[/color][/size][/font][/color]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
[quote name='nano511' timestamp='1313165079' post='4848276']
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.
[/quote]

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
[quote name='nano511' timestamp='1313168213' post='4848306']
I seed the number generator in the monster's constructor.
[/quote]

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
[quote name='nano511' timestamp='1313168717' post='4848319']
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
[/quote]

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
[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() );[/quote]
And what advantages does that have compared to calling srand(time(0)) in main?

Share this post


Link to post
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
[code]
if( monster is on the left side of the player and player is looking left )
{
if( player presses space )
{
zombie is hurt;
}
}
[/code]

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


Link to post
Share on other sites
[quote name='nano511' timestamp='1313169193' post='4848324']
So, what is the usual approach to 2D gun games concerning shooting?
[/quote]

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:

[code]
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
[/code]

Share this post


Link to post
Share on other sites
[quote name='Wooh' timestamp='1313169154' post='4848323']
[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() );[/quote]
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

Share this post


Link to post
Share on other sites

This topic is 2314 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this