dimanche 1 septembre 2019

If else logic help (javaScript)

Im trying to set a condition that if the Chosen Player var is false (it is in the global scope) then on click of a given element, change that elements color and then set the Chosen player var to true. Limiting the click-red to one element on the page.

However something must be going wrong with either syntax of logic, as anytime I select the element I can still change to color of any other element.

if(CharhasbeenSelelcted === true) {
    console.log("Now select who you wish to attack");


//and if You have not chosen an enemy yet
        if(ChooseEnemy === false) {
//the enemy player you clicked will turn red
        $(".Enemyplayer").on("click", function(){   
        //this changes the class you've clicked to be its own entity so it 
can be singled out futher
        $(this).attr("class", "ChosenAttack")


        $(".ChosenAttack").css("background-color", "red")
        ChooseEnemy = true;

    })


        }if(ChooseEnemy === true){
            return;
        }

}else {

}



});

Aucun commentaire:

Enregistrer un commentaire