简体   繁体   中英

Execute once every value is reached C++

The title doesn't exactly explain fully what I'm trying to do.

Basically I'm making a game in SFML and I want a boss to appear once the player has reached a certain score, let's say 1000. However I want the loop to only make the boss appear once (currently its spawning the boss like crazy once the 1000 point criteria has been met) How do I have so that the boss appears once? Currently this is code I have.

    //Spawns Boss
    if (Player1.score >= 1000)
    {
        boss1.rect.setPosition(generateRandom(window.getSize().x), -100);
        enemyArray.push_back(boss1);
    }

Also I want the boss to only appear once every 1000 score. For example once the player reaches 1000 score a boss appears then another at 2000 and vice versa. I haven't thought of a code to do this yet. I don't think extra code is needed but if you do require it let me know

*Edit I used a bit of the implementation suggested by @RedFur however it only works once 1000 is reached, once 2000 is reached the code doesn't execute once again. I'm assuming it won't work at 3000 either but I never really tried, any suggestions?

*Edit2 Made use of RedFur's solution in addition to a flag to see if a boss has been spawned. Worked flawlessley. Fixed Code below:

boss_appearances = 0;
bool flag = false;
if (flag == false){
    if (Player1.score/1000 > boss_appearances) {
      boss_appearances++;
      do stuff;
      flag = true
    }
}

Tracking the number of times the boss appeared is one way to go: Every time the player reaches 1000, boss appears once. Then do again for every multiple of 1000.

boss_appearances = 0;
if (Player1.score/1000 > boss_appearances) {
  boss_appearances++;
  do stuff;
}

Since 1000 in an integer, the result is an integer with truncated decimals (assuming score is also an int). So the first time the player reaches between 1000 and 2000 Player1.score/1000 would equal 1, etc.

int numBossSpawns = 1;
if (Player1.score >= numBossSpawns * 1000)
   {
       boss1.rect.setPosition(generateRandom(window.getSize().x), -100);
       enemyArray.push_back(boss1);
       numBossSpawns++;
   }

something like that might work, I think similar solutions have already been posted though.

After looking at some of the comments to the other solutions, a simple quick fix to the boss monster being spawned each frame per second would be to add a flag that prevents the boss monster from being spawned again after the first time.

This flag could be set true when the boss is initially spawned, then reset to false when he is killed. Or something along those lines...

// flag initialised to true. The boss isn't alive yet.
bool bossNotAlive = true;

/* check if score is a multiple of 1000, not the most effective method    */
/* if the players score goes from 990 to 1090 without ever hitting 1000,  */
/* it wouldn't trigger a boss spawn.                                      */
if (bossNotAlive && Player1.score % 1000 == 0) 
{
    // since the boss is about to be spawned, 
    bossNotAlive = false;
    boss1.rect.setPosition(generateRandom(window.getSize().x), -100);
    enemyArray.push_back(boss1);
}

// Something in the boss to check death.
if (Boss.health < 0)
{
    bossNotAlive = false;
    // Call Destructor or something
}

Something you might want to look at is the use of some kind of game state manager, something dedicated to tracking the current state, what happens and when.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM