Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make if statement true only once?

Tags:

javascript

How do I make an if statement run only once. I am making a game in javascript and once the users score is 5 I want to add a new enemy into the array. The problem I am having is once the if statement is true it keeps adding new enemies over and over until the whole screen is covered with enemies. I want it to add only 1 new enemy when score is 5 then another when score is 10 and so on. I cannot think of a way to accomplish this. Thank You.

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();
   }
like image 935
Sean Avatar asked Mar 20 '17 05:03

Sean


6 Answers

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

If you are running the code inside of a function, make sure that addNew is declared somewhere that it will persist between calls.

like image 133
trognanders Avatar answered Oct 05 '22 19:10

trognanders


You need a simple latch. Try adding a boolean variable called isEnemyAddedAtFive and set it to false as it's default value.

var isEnemyAddedAtFive = false;

Then at your check point test this value and if not latched, act and latch:

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

Good Luck!

like image 38
Monza Avatar answered Oct 05 '22 17:10

Monza


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

Try this one.

like image 43
Abhilash Augustine Avatar answered Oct 05 '22 17:10

Abhilash Augustine


This is a little more complex than you are currently coding. There are a few ways you could interpret this. Firm your requirements.

Do you want to only add an enemy if the score is divisible by 5... Meaning only 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 track a total score and a level/segmented score representing the score since the program last took action on 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 get more complex as you as more features. The best first step is to define clear requirements before coding as one retirement may influence how you implement another

like image 40
Matt Avatar answered Oct 05 '22 17:10

Matt


//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();
like image 29
blackmiaool Avatar answered Oct 05 '22 17:10

blackmiaool


What comes first to mind after seeing your code is that:

  • It should be

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

    for 3 enemies

  • You are calling your code (i.e .js file multiple times). This means that score resets to 0 each time it is called and score==5 condition occurs multiple times.

Solution:

Change your logic such that the .js file is called only once and store the score for the entire duration of the game.

Hope this helps

like image 21
XChikuX Avatar answered Oct 05 '22 17:10

XChikuX