How to do if the statement is true only one time?

How to do an if statement only once. I am making a game in javascript and as soon as the user types 5, I want to add a new enemy to the array. The problem I ran into is once the if statement is true that it keeps adding new enemies over and over until the whole screen is covered by enemies. I want it to add only 1 new enemy when the count is 5, then another when the count is 10 and so on. I cannot think of how to do this. Thank.

Sketch.js

   var score = 0;

   //3 three enemies once game starts
   function StartingEnemies() { 
      for (var i = 0; i < 2; i++) {
        enemies[i] = new BasicEnemy();
      }
   }


   //add new enemies into the array
   function spawnNewEnemies() {
     enemies.push(new BasicEnemy());
   }


   if (score == 5) {
     spawnNewEnemies();
   }

      

0


source to share


6 answers


var addNew=true;
if (score == 5 && addNew) {
  spawnNewEnemies();
  addNew=false;
}

      



If you are using code inside a function, make sure addNew is declared somewhere so it will persist between calls.

+2


source


You need a simple latch. Try adding a boolean variable named isEnemyAddedAtFive and setting it to false by default.

var isEnemyAddedAtFive = false;

      

Then on your breakpoint, check that value, and if it's not latched, act and latches:



if( score == 5 && !isEnemyAddedAtFive ) {
    addEnemy();
    isEnemyAddedAtFive = true;
}

      

Good luck!

+4


source


if ( score != 0 && score % 5 == 0) {
     spawnNewEnemies();
}

      

Try it.

+3


source


This is a little more complicated than what you are currently coding. There are several ways to interpret this. Strengthen your requirements.

You only want to add the enemy if the score is divisible by 5 ... Only meaning when the score ends in 0 or 5 (5, 10, 15, 20, etc.):

If (score % 5 == 0) {
  enemies.push(new BasicEnemy());
}

      

Otherwise, you may need to keep track of the total score and the level / segment score representing the score since the last game entered the game.

// Initialization
var totalScore = 0;
Var currents core = 0;

// Game event loop
while (player.lifeState > 0) {

// Check current score is 5 or more on every iteration of the game event loop
if (currentScore >= 5) {
// Handle score
spawnNewEnemies();
// Update scoring
totalScore += currentScore;
// Reset currentScore (or not if you want to do more stuff in the level before resetting but this may change the above logic)
currentScore = 0;
}

      

Your logic will be more complex as you have more options. The best first step is to define clear requirements before coding, as one retirement can affect the implementation of another

+2


source


//post this snippet at some top level of your code.
var onceMap = {};

function once(key) {
  if (onceMap[key]) {
    return false;
  } else {
    onceMap[key] = true;
    return true;
  }
}
var score = 10;

//do this in your task function
function task() {
  if (score >= 5 && once("score5")) {
    console.log("score 5 action");
  }
  if (score >= 10 && once("score10")) {
    console.log("score 10 action");
  }
}
task();
task();
task();
      

Run codeHide result


+1


source


Things to remember after looking at the code:

  • It should be

    for (var i = 0; i < 3; i++)
    
          

    for 3 enemies

  • You are calling your code (like a .js file multiple times). This means that the counter is reset to 0 every time it is called and the count state == 5 occurs multiple times.

Decision:

Change your logic so that the .js file is called only once and keeps the score throughout the game.

Hope it helps

+1


source







All Articles