constructor({
keysMap = new Map([
["up", "ArrowUp"],
["right", "ArrowRight"],
["down", "ArrowDown"],
["left", "ArrowLeft"],
]),
x = 20,
y = 20,
speed = 0.5,
} = {}) {
Object.assign(this, { touches, x, y, speed });
...
this.moveX = 0;
this.moveY = 0;
}
...
}
https://es6console.com/jpcwjhve/
------------ Amélioration du code
Méthode static !
class Player {
constructor( {keysMap, x, y, speed} ) {
Object.assign(this, {
keysMap,
x,
y,
speed
});
this.moveX = 0;
this.moveY = 0;
...
}
static create({
keysMap = new Map([
["ArrowUp", "up"],
["ArrowRight", "right"],
["ArrowDown", "down"],
["ArrowLeft", "left"],
]),
x = 100,
y = 100,
speed = 0.5,
} = {}) {
return new Player({keysMap, x, y, speed})
}
...
}
appel
Player.create();
Player.create({
keysMap: new Map([
["z", "up"],
["d", "right"],
["x", "down"],
["q", "left"],
]),
speed: 2
});
https://es6console.com/jpdtv6zx/