3 回答

TA貢獻1951條經驗 獲得超3個贊
你有不同的錯誤。
首先,您必須使用new Player()
而不僅僅是player
.
其次,你應該使用這個:
Players.push(player1);
而不是這個:
Players.push([player1]);
第一個將 player1 推送到 Players 數組,第二個將僅包含 player1 的新數組推送到數組 Players。您還必須使用它來添加 player2。

TA貢獻1797條經驗 獲得超6個贊
這是因為您沒有Player正確創建一個。而不是使用let player1 = Player,使用let player1 = new Player()。此外,您需要創建一個Players數組。最后,你需要Player上課。
class Player {
constructor(sprite, coords, name) {
this.sprite = sprite;
this.coords = coords;
this.name = name;
}
}
var Players = []; // If you want to access this array
// from outside thefunction, keep it here.
// Otherwise, move it in the LoadPlayers function
function LoadPlayers() {
let player1 = new Player(0, {
M: 0,
X: 6,
Y: 1
}, "jimpie");
Players.push(player1);
let player2 = new Player(0, {
M: 0,
X: 1,
Y: 17
}, "kolien")
Players.push(player2);
console.log("total players: " + Players.length)
console.log("Player 1 name: " + Players[0].name)
console.log("Player 2 name: " + Players[1].name)
}
LoadPlayers();

TA貢獻1831條經驗 獲得超4個贊
function Player(sprite, coords, name){
this.sprite = sprite;
this.coords = coords;
this.name = name;
}
let Players = []
function LoadPlayers(){
console.log("total players: " + Players.length)
let player1 = new Player(1, [32, 15, 14], "jimpie")
Players.push(player1);
let player2 = new Player(1, (1, 1, 1), "kolien")
Players.push(player2);
console.log("total players: " + Players.length)
console.log("Player 1 name: " + Players[0].coords)
console.log("Player 2 name: " + Players[1].name)
var allcoords = Players[0].coords
var xcoord = allcoords[1]
var ycoord = allcoords[2]
var mapnum = allcoords[0]
console.log("map: " + mapnum + " x: " + xcoord + " y: " + ycoord);
}
這很好用!
添加回答
舉報