选择数组中的对象时出错

时间:2016-01-07 14:54:04

标签: javascript arrays function object

我正在为棋盘游戏应用编写代码。我编写了一段简单的代码来确定玩家在数学家中的位置。 mathrandom的结果告诉数组选择4个对象中的1个(玩家)并将结果添加到他们的位置。我还在函数的开头添加了一段代码,以便在数组中向上移动并选择下一个玩家,这样每次点击“掷骰子”按钮时,其他玩家都会获得更多的价值。

问题在于,在我完成整个阵列并返回[0]位置后,它会运行代码两次,以便第一个玩家获得该功能两次,而所有其他玩家只获得一次。

这是我的代码:

var players = [
{name: "Player 1", positie: 0},
{name: "Player 2", positie: 0},
{name: "Player 3", positie: 0},
{name: "Player 4", positie: 0}
];
var position = 0;
var currentPlayer = players[position];

function rolClick(){
currentPlayer = players[position++];
if (position > players.length){
    position = 0;
    currentPlayer = players[position];
}
var rollen = Math.floor(Math.random() * 6) + 1;
if (rollen === 1){
    currentPlayer.positie += 1;
    console.log("het is 1")
}else if(rollen === 2){
    currentPlayer.positie += 2;
    console.log("het is 2")
}else if(rollen === 3){
    currentPlayer.positie += 3;
    console.log("het is 3")
}else if(rollen === 4){
    currentPlayer.positie += 4;
    console.log("het is 4")
}else if(rollen === 5){
    currentPlayer.positie += 5;
    console.log("het is 5")
}else if(rollen === 6){
    currentPlayer.positie += 6;
    console.log("het is 6")
}else {
    console.log("Oops, er is iets fout gegaan.")
};
console.log(currentPlayer);
};

2 个答案:

答案 0 :(得分:0)

我认为您的问题是,在调用currentPlayer函数之前首先设置rolClick(),然后在“骰子”之前重置/更新currentPlayer。滚了。请参阅下面的评论部分......

var players = [
    {
        name: 'Player 1',
        positie: 0
    },
    {
        name: 'Player 2',
        positie: 0
    },
    {
        name: 'Player 3',
        positie: 0
    },
    {
        name: 'Player 4',
        positie: 0
    }
];
var position = 0;

// currentPlayer is set to player 1
var currentPlayer = players[position];

function rolClick() {
    // currentPlayer is now set to player 2
    currentPlayer = players[position++];

    if (position // ... etc etc ...

}

要在currentPlayer功能的末尾而不是在开头处结束此更新rolClick()

此外,您可能希望使用switch()语句来替换if-else-if过滤列表。有关详情,请参阅MDN HERE

希望有所帮助。 :)

答案 1 :(得分:0)

<强> HTML

var players = [
{name: "Player 1", positie: 0},
{name: "Player 2", positie: 0},
{name: "Player 3", positie: 0},
{name: "Player 4", positie: 0}
];
var position = 0;
var currentPlayer = null;

function rolClick(){
  if (position >= players.length){
      position = 0;
  }
  console.log("Position", position);
  currentPlayer = players[position++];

   var rollen = Math.floor(Math.random() * 6) + 1;
   currentPlayer.positie += rollen;
   $("span").html("Je hebt " + rollen + " gerold voor " + currentPlayer.name);
};

$("button").click(function() {
    rolClick();
});

<强>的Javascript

var players = [{
  name: "Player 1",
  positie: 0
}, {
  name: "Player 2",
  positie: 0
}, {
  name: "Player 3",
  positie: 0
}, {
  name: "Player 4",
  positie: 0
}];
var position = 0;
var currentPlayer = null;

function rolClick() {
  if (position >= players.length) {
    position = 0;
  }
  console.log("Position", position);
  currentPlayer = players[position++];

  var rollen = Math.floor(Math.random() * 6) + 1;
  currentPlayer.positie += rollen;
  $("span").html("Je hebt " + rollen + " gerold voor " + currentPlayer.name);
};

$("button").click(function() {
  rolClick();
});

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>
  Rol de dobbelsteen
</button>
<span></span>
&#13;
int iean=0;

......

inputEan = (EditText) findViewById(R.id.inputEan);
    inputEan.setOnFocusChangeListener(new OnFocusChangeListener() {          
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus){

                iean=0;

            }

        }
    });

inputEan.addTextChangedListener(new TextWatcher() {

           public void afterTextChanged(Editable s) {


               iean=s.length();
               //if iean ==  0 it's scanner
               //if iean == 13 it's keyboard
           }

           public void beforeTextChanged(CharSequence s, int start, 
             int count, int after) {
           }

           public void onTextChanged(CharSequence s, int start, 
             int before, int count) {
           }
          });
&#13;
&#13;
&#13;