我是firebase的新手,并且"执着#34;数据。我正在尝试制作一个石头剪刀游戏,可以让两个玩家登录并在其他玩家屏幕上显示对方的登录名。我也可以让第一个玩家选择他们的选择,然后在Firebase中将game.turn变量增加到2并在第二个玩家本地数据上增加(因为它会在控制台中将其记录下来)。但是,我的代码将不允许进入第二个if语句,其中game.turn == 2打开第二个玩家的按钮监听器(我知道这是因为它没有打印出该部分中的console.log )。
我已经研究并尝试了几种不同的东西,从嵌套ifs到其他ifs将数据发送到Firebase太多次并创建无限循环。如果有人能帮助我,我会非常感激!我在这部分的代码如下(我试图省略我认为不需要的东西。我确实称这些函数启动它们,但我没有包含它。):
choice: function() {
var playersRef = game.dataInfo.child(game.players);
var player1Ref = playersRef.child(game.player1);
var player2Ref = playersRef.child(game.player2);
// add turn to firebase
game.turn++;
game.dataInfo.update({
turn: game.turn
}); // end firebase update
// if it is turn 1 then player 1 picks
if (game.turn == 1) {
// on click for player 1 choice
$("#choices1").on('click', '.piece', function() {
// updates the pick for player 1 in firebase
player1Ref.update({pick: $(this).data('name')});
$('#choices1').children().not(this).hide();
game.turn++;
// update turn to firebase
game.dataInfo.update({
turn: game.turn
}); // end firebase update
}); // end of on click for player 1 choice
} // end of if player 1 hasn't picked
// if it is turn 2 then player 2 picks
if (game.turn == 2) {
console.log("I made it!");
// on click for player 2 choice
$('#choices2').on('click', '.piece', function() {
// updates the pick for player 2 in firebase
player2Ref.update({pick: $(this).data('name')});
$('#choices2').children().not(this).hide();
game.turn++;
// update turn to firebase
game.dataInfo.update({
turn: game.turn
}); // end firebase update
}); // end of click for player 2 choice
} // end if it is turn 2 then player 2 picks
}, // end of choice function
// updates local variables within game object from firebase
updateVar: function () {
game.dataInfo.on("value", function(snapshot) {
game.turn = snapshot.val().turn;
console.log(game.turn);
game.pick = snapshot.val().players[1].pick;
console.log(game.pick);
game.pick2 = snapshot.val().players[2].pick;
console.log(game.pick2);
game.wins = snapshot.val().players[1].wins;
console.log(game.wins);
game.losses = snapshot.val().players[1].losses;
console.log(game.losses);
game.ties = snapshot.val().players[1].ties;
console.log(game.ties);
game.wins2 = snapshot.val().players[2].wins;
console.log(game.wins2);
game.losses2 = snapshot.val().players[2].losses;
console.log(game.losses2);
game.ties2 = snapshot.val().players[2].ties;
console.log(game.ties2);
});
}, // end updateVar function
HTML:
<div class="container">
<div class="row">
<div class="col-sm-12">
<h1>Rock, Paper, Scissors, Lizard, Spork:</h1>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div id="nameForm">
<form>
<div class="form">
<input type="text" placeholder="Name" id="name-input"><br>
</div>
<div class="form">
<input id="connect" type="submit" value="Submit">
</div>
</form>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-4" id="player1">
<p id="wait1">Waiting for player 1</p>
<div id="choices1"></div>
<p id="score1"></p>
</div>
<div class="col-sm-4" id="winner">
</div>
<div class="col-sm-4" id="player2">
<p id="wait2">Waiting for player 2</p>
<div id="choices2"></div>
<p id="score2"></p>
</div>
</div>
<div class="row">
<div class="col-sm-6" id="disconnect1">
</div>
<div class="col-sm-6" id="disconnect2">
</div>
</div>
</div>
答案 0 :(得分:1)
在第一个if
语句之前,您正在增加game.turn
:
// add turn to firebase
game.turn++;
game.dataInfo.update({
turn: game.turn
}); // end firebase update
如果输入函数时game.turn
为2,它是否会增加到3,因此永远不会输入第二个if
语句?
我建议你删除上面提到的game.turn++;
行。