试图改变团队的JavaScript棒球计划

时间:2017-04-07 13:28:42

标签: javascript

我正在使用code.org javascript库。我试图拥有它所以它根据两个选定的一次3个出局来改变团队。我尝试声明team1team2并将它们设置为与所选的复选框相同但不能正常工作的复选框。

团队名称在标签上不会像第一次选择团队时那样发生变化。我不太确定该如何做,但主要目标是在当前球队获得3次出局时让球队名称改变。玩家选择功能中使用了一个标签,显示所选的第一个小组。

var outs = 0;
var team1 = "";
var team2 = "";
var strikes = 0;
var balls = 0;
var fouls = 0;
var inning = 0;
var hit = 0;
var currentPlayer = team1, team2;

//Player Selection
onEvent("btnStart","click", function() {
  var chkBoxs = ["Yankees", "Boston", "Astros"];
  var selected = [];
  for (var index = 0; selected.length < 2 && index < chkBoxs.length; index++) {
    if (getChecked(chkBoxs[index])) { selected.push(index); }
  }
  setScreen("game");
  if (selected.length == 2) {
    console.log("The Teams are: " + chkBoxs[selected[0]] + " and " + chkBoxs[selected[1]]);
  }
  if (chkBoxs[selected[0]]) {
    setText("lblTeamGame",chkBoxs[selected[0]]);
    team1 = chkBoxs[selected[0]];
    team2 = chkBoxs[selected[1]];
  } else {
    setText("lblTeamGame",chkBoxs[selected[1]]);
  }
});

//Pitching Rules
function Count() {
  if (balls == 4)
  {
    console.log("Walk");
    setText("lblBallCount", 0);
    setText("lblStrikeCount", 0);
    balls = 0;
    strikes = 0;
  }
  if (strikes == 3)
  {
    console.log("Strike Out");
    outs++;
    setText("lblStrikeCount", 0);
    setText("lblOutCount", outs);
    setText("lblBallCount", 0);
    strikes = 0;
    balls = 0;
  }
  if (outs == 3)
  {
    inning++;
    setText("lblInningCount", inning);
    switchPlayer();
  }
  if(hit)
  {
    setText("lblStrikeCount", 0);
    setText("lblBallCount", 0);
    strikes = 0;
    balls = 0;
  }
}
//Switch Teams 
function switchPlayer() {
  if(currentPlayer == team1) {
    currentPlayer = team2;
    showElement("player2_highlight");
    hideElement("player1_highlight");
  } else {
    showElement("player1_highlight");
    hideElement("player2_highlight");
    currentPlayer = 1;
  }
  console.log("current player is: " + currentPlayer);
}

1 个答案:

答案 0 :(得分:0)

这可能是你的问题:

function switchPlayer(){
 if(currentPlayer==team1){
     currentPlayer=team2;
     showElement("player2_highlight");
     hideElement("player1_highlight");
 } else {
   showElement("player1_highlight");
   hideElement("player2_highlight");
   currentPlayer=team1; // Specify team1 instead of just 1
 }
 console.log("current player is: "+currentPlayer);
}

此外,在切换团队后,您需要重置出局次数。

   if(outs==3)
   {
      inning++;
      setText("lblInningCount",inning);
      switchPlayer();
      outs=0;
   }