Why is coords
always 0,0
no matter what I put for the values in let $newTileData = new tile([1,3],{})
?
class tile { constructor(coords, layers) { this.coords = [0, 0]; this.layers = [ {sprite: ""}, {sprite: ""}, {sprite: ""}, //player "layer" is here? {sprite: ""}, {sprite: ""}, {sprite: ""} ] } } let mapdata = { mapName: "Test", tiles: [ ] }; let $newTileData = new tile([1,3],{}) mapdata.tiles.push($newTileData); console.log(mapdata);
coords
is always 0,0 no matter what you put for the values because you declare: this.coords = [0, 0];
in the class. You are ignoring the input value when you do that. Same as ignoring layers
:
this.layers =
[
{sprite: ""},
{sprite: ""},
{sprite: ""},
//player "layer" is here?
{sprite: ""},
{sprite: ""},
{sprite: ""}
]
//Now the function(layers) doesn't do anything because layers is ignored.
You want a constructor that actually uses the constructor parameters rather than only setting default values. If the default values are necessary to keep in place (to maintain the working empty constructor new tile()
), code can be added to accommodate that.
// this would need to be used with values, new title([0,0], {layerdata})
class tile {
constructor(coords, layers) {
this.coords = coords;
this.layers = layers;
}
}
This does what I had in mind... Thanks to @quicVO for leading me to this.
let $newTileCoords = [1,3];
let $newTileData = new tile()
$newTileData.coords = $newTileCoords;
mapdata.tiles.push($newTileData);
Will leave unanswered in case someone comes up with a more appropriate way to instantiate a class with values.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.