我正在學習javascript,我想在一個函數中填充數組,並將其作爲第二個函數中的參數傳遞。我有下面的代碼,但由於某種原因,它不工作。無法在函數中傳遞數組javascript
function gameplay(tmp) {
for (var i = 0; i < tmp.length; i++) {
if (xTrans >= tmp[0] - 1
&& xTrans < tmp[0]
&& zTrans <= tmp[1] - 1
&& zTrans > tmp[1])
{
//some code here
}
}
}
function fillMap() {
var bounds = new Array();
for (var y = 0; y < map.length; y++) {
for (var x = 0; x < map[0].length; x++) {
if (map[y][x] == '1') {
bounds[length] = 4 * x;
length++;
bounds[length] = -y - 4 * y;
length++;
}
}
}
return bounds;
}
和我這樣調用:
var tmp = fillMap();
gameplay(tmp);
在此先感謝..
你是否得到某種錯誤信息,或者輸出/結果是不是你所期望的? –
確保您的代碼完全運行。 'length'變量沒有被定義。我假設你想'bounds.push(...)'來代替。如果您對JS中的數組不熟悉,那麼我建議您使用一個教程:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Predefined_Core_Objects#Array_Methods。 –
長度被定義,當我試圖打印數組時,我得到的消息是空的。 – user3478067