1

how can i read the name of the player in de list?

let Players = []

let Player = {
    sprite: 1,
    coords: {M: 0, X: 0, Y: 0},
    name: ""
}

function LoadPlayers(){

console.log("total players: " + Players.length)

let player1 = Player
player1.sprite = 0
player1.coords = {M: 0, X: 6, Y: 1};
player1.name = "jimpie"

Players.push([player1]);



let player2 = Player         
player2.sprite = 0
player2.coords =  {M: 0, X: 1, Y: 17};
player2.name = "kolien"

Players.push([player2]);

console.log("total players: " + Players.length)

console.log("Player 1 name: " + Players[1].name)
console.log("Player 2 name: " + Players[0].name)
}

I'm getting 'undefined' now and i want to read out the player details from a specific player in the list.

3
  • 1
    You're missing some news in your let _ = Player lines, it seems. Commented Jun 17, 2020 at 17:46
  • Where are you getting undefined? Is Players defined somewhere else in the code? What about Player? Commented Jun 17, 2020 at 17:46
  • Also, you haven't instantiated a Players array (not in that code, at least) Commented Jun 17, 2020 at 17:48

3 Answers 3

1

You have different errors.

First, you must use new Player() instead of just player.

Second, you should use this:

Players.push(player1);

instead of this:

Players.push([player1]);

The first one pushes player1 to the array of Players, the second one pushes a new array that only contains player1 to the array Players. You must also use this to add player2.

Sign up to request clarification or add additional context in comments.

9 Comments

The code would still work if it remained as Players.push([player1]). However, to access the player, one would use Players[i][0] instead of Players[i].
Also, Player() needs a new
Yes, but I don't think that's what OP wanted.
Thank you @AlexH!
In response to 'Yes, but I don't think that's what OP wanted': It could go either way. It's best to show both methods and detail the results of each one, so the OP can choose which was best.
|
1

It's because you're not creating a Player correctly. Instead of using let player1 = Player, use let player1 = new Player(). Also, you need to create a Players array. Lastly, you'll need to make a Player class.

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();

6 Comments

You need to define a Player class for this to work (if that's what you mean)
sorry, forgot to put all code lines in. made an edit in my first post. it says with new Player() that player is not a constructor
If the answer was correct, please accept it (click the checkmark) to show that it solved your problem. This serves as an indicator to anyone else who finds the question which answer worked.
Can you show the code of your Player class in the question?
I did, there is no player class. i have: let Player = { sprite: 1, coords: {M: 0, X: 0, Y: 0}, name: "" } it says two times Player 1 name: kolien, Player 2 name: kolien in the console
|
0

thanks @AlexH

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);

}

this works fine!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.