jeudi 20 mai 2021

How to iterate into an array inside of an if statement

I'm trying to add values to an array via a condition within an if function. Here is the estate agent in question along with the other parts that I want to integrate:

function keyDown(e){
    var arr= [];
    if(initialTime== 0){
        
        return;
    }
    
    if(e.keyCode == Keyboard.LEFT) {
        ship.move(-25, 0);
            
    }
    if(e.keyCode == Keyboard.RIGHT){
        ship.move(25,0);
    }
    if(shipX == enemyX1 && e.keycode== Keyboard.letter('a')){
        scoreCount();
    }
    if(shipX == enemyX2 && e.keycode== Keyboard.letter('a')){
        scoreCount2();
    }
    }
    function scoreCount(){
        arr.push(60);
    }

    function scoreCount2(){
        arr.push(20);
    }

In this code, I'm trying to get it so that when the x coordinate of the ship object equals the x coordinate of either enemy 1 or 2 and the letter a is pressed that it'll iterate a score of either 60 or 20 into an array. I will then add up all instances of this array at the end of the program to see if the user has failed or won the game.

I've gotten to the point where it just prints the first value of 60 over and over again and using a return stops that but I wanted to count each individual time that the if the condition is met and put that into the array and then add up of all the values in the array to get a final score. Any insight on how to do that would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire