hey guys i have a problem with my code am trying to use a do while to create 5 enemies whithin my vector and i expect it to stop pushing back when m=5...but its not working and its still creating more than 5 enemies endlessly...am using sfml 2.3.1 please help.....thanx in advance
int m=0;
do
{
if((Timer1.asSeconds()>= 1))
{
clock1.restart();
class random randoms;
enemy1.rect.setPosition(randoms.random_function(1000),
randoms.random_function(800));
enemyArray.push_back(enemy1);
}
m++;
}
while(m<=5);
First to declaration:I'm a newbie and I don't know what your function is.
But, in your case.I reckon it is your if series caused the problem.
Try to instead of this below:ps.apologize to my poor English..
int m = 0;
do{
if(Timer1.asSeconds()>=1){
clock1.restart();
class random randoms;
enemy1.rect.setPositon(randoms.random_function (1000),
randoms.random_function(800));
enemyArray.push_back(enemy1);
m++;//this is the change
}
}while(m<5)//there I think you should reset the edge or you may circle 6loops
It's a matter of style but I find this way to do the same as other suggestions comfortable. Depends also on the sequence of operations but that's another thing that may/may not be relevant.
1 2 3 4 5 6 7 8 9 10 11 12 13
int m = 0;
while ( m < 5 )
{
if((Timer1.asSeconds()>= 1))
{
clock1.restart();
class random randoms;
enemy1.rect.setPosition( randoms.random_function(1000), randoms.random_function(800) );
enemyArray.push_back(enemy1);
}
m++;
}