函数内的布尔变量不会在全局范围内更改所述变量的值

时间:2019-09-01 16:07:45

标签: javascript function boolean-logic

我正在尝试为我所在的类创建一个基本的RPG游戏,当前遇到布尔变量问题。我设置了一个单击函数,以运行特定的命令列表,然后将全局范围内的布尔值var更改为true。然后,这将触发if if(Var === true),否则将触发一组新命令。但是,我必须缺少一些东西,因为除非在第一个函数的范围内设置if语句,否则console.log不会启动。

var CharhasbeenSelelcted = false;


//first on click function for getting player boxes aligned properly
$(".Pcontainer").on("click", function(){

if(CharhasbeenSelelcted === false){
//setting the attribute of whatever you've clicked so the append command 
given no longer applies to it
$(this).attr( "class", "Chosenplayer")

$('.Pcontainer').attr( "class", "Enemyplayer")

//append the newly assigned class to its own area of the screen
$("#MainCharcterArea").append( $(".Chosenplayer") );

//append all other boxes to enemy area
$('#EnemyContain').append( $('.Enemyplayer') );

} else {
} 

CharhasbeenSelelcted = true;

});

//-----------------------scope issue I can't quite figure out 
if(CharhasbeenSelelcted === true) {

        console.log("attack button working");

    };

到目前为止,没有错误消息可见,这实际上更令人沮丧。大声笑

0 个答案:

没有答案