class Joueur extends Sprite { public scene_: Jeu; public vitesse_: number; public vitesseMax_: number; public timerAnimation: number | null; public ecouteurClavier_: any; public ecouteurClavierStop_: any; private touchesEnfoncees: { [key: string]: boolean }; public constructor(element: HTMLElement, scene: Jeu) { super(element); this.scene_ = scene; this.timerAnimation = null; this.vitesse_ = 6; this.vitesseMax_ = 15; this.touchesEnfoncees = {}; this.ecouteurClavier_ = (evt: KeyboardEvent) => this.deplacer(evt); this.ecouteurClavierStop_ = (evt: KeyboardEvent) => this.stopperDeplacement(evt); } public figer() { window.removeEventListener("keydown", this.ecouteurClavier_); window.removeEventListener("keyup", this.ecouteurClavierStop_); clearInterval(this.timerAnimation); } public animer() { window.addEventListener("keydown", this.ecouteurClavier_); window.addEventListener("keyup", this.ecouteurClavierStop_); } private deplacementGauche() { this.setX(this.getX() - this.vitesse_); } private deplacementDroite() { this.setX(this.getX() + this.vitesse_); } private deplacementHaut() { this.setY(this.getY() - this.vitesse_); } private deplacementBas() { this.setY(this.getY() + this.vitesse_); } /* public deplacer(evt: KeyboardEvent) { if (this.timerAnimation !== null) return; console.log(evt.key); if (evt.key === "ArrowLeft") { this.timerAnimation = setInterval(() => this.deplacementGauche(), 1000 / 120); } else if (evt.key === "ArrowRight") { this.timerAnimation = setInterval(() => this.deplacementDroite(), 1000 / 120); } else if (evt.key === "ArrowUp") { this.timerAnimation = setInterval(() => this.deplacementHaut(), 1000 / 120); } else if (evt.key === "ArrowDown") { this.timerAnimation = setInterval(() => this.deplacementBas(), 1000 / 120); } }*/ private mettreAJourDeplacement() { //Mouvement en diagonales if (this.touchesEnfoncees["ArrowLeft"] && this.touchesEnfoncees["ArrowUp"]) { this.deplacementGauche(); this.deplacementHaut(); } else if (this.touchesEnfoncees["ArrowLeft"] && this.touchesEnfoncees["ArrowDown"]) { this.deplacementGauche(); this.deplacementBas(); } else if (this.touchesEnfoncees["ArrowRight"] && this.touchesEnfoncees["ArrowUp"]) { this.deplacementDroite(); this.deplacementHaut(); } else if (this.touchesEnfoncees["ArrowRight"] && this.touchesEnfoncees["ArrowDown"]) { this.deplacementDroite(); this.deplacementBas(); //Mouvments normaux } else if (this.touchesEnfoncees["ArrowLeft"]) { this.deplacementGauche(); } else if (this.touchesEnfoncees["ArrowRight"]) { this.deplacementDroite(); } else if (this.touchesEnfoncees["ArrowUp"]) { this.deplacementHaut(); } else if (this.touchesEnfoncees["ArrowDown"]) { this.deplacementBas(); } } public deplacer(evt: KeyboardEvent) { this.touchesEnfoncees[evt.key] = true; if (this.timerAnimation === null) { this.timerAnimation = setInterval(() => this.mettreAJourDeplacement(), 1000 / 144); } } //J'ai crée cette fonction avec ChatGPT //J'avais trouvé tout seul le moyen d'arrêter mon personnage avec un deuxième écouteur, mais en appuyant sur deux touche simulatement, cela faisait n'importe quoi, j'ai donc rectifié ma fonctione de base qui était celle-ci : /* public nondeplacer(evt : KeyboardEvent) { console.log(evt.key); if (evt.key == "ArrowLeft") {clearInterval(this.timerAnimation);} else if (evt.key == "ArrowRight") {clearInterval(this.timerAnimation);} else if (evt.key == "ArrowUp") {clearInterval(this.timerAnimation);} else if (evt.key == "ArrowDown") {clearInterval(this.timerAnimation);} }*/ public stopperDeplacement(evt: KeyboardEvent) { delete this.touchesEnfoncees[evt.key]; if (Object.keys(this.touchesEnfoncees).length === 0 && this.timerAnimation !== null) { clearInterval(this.timerAnimation); this.timerAnimation = null; } } /*public setLimites() { this.xmax_ = 0; this.xmin_ = 0; this.ymax_ = 0; this.ymin_ = 0; }*/ }