我正在使用Typescript为学校学生做一个小游戏项目。 我正在尝试向事件侦听器中添加一个函数,但事件侦听器将所有参数都视为无效。
public hintShopHandler(xBox: number, yBox: number) {
let number1: number = xBox;
let number2: number = yBox;
window.addEventListener("click", this.hintShopHandler(number1, number2))
}
private hintShopListener(xBox: number, yBox: number): any {
let event: MouseEvent;
for (let index = 0; index < 3; index++) {
if (this.shopScreen == true) {
this.canvas.writeTextToCanvas(`You have ${this.hints} hints`, 50, this.canvas.getCenter().X + 450, this.canvas.getCenter().Y - 100, "black", "center", "Old English Text MT")
this.canvas.writeTextToCanvas(`${this.player.getScore()} points`, 50, this.canvas.getCenter().X - 410, this.canvas.getCenter().Y - 250, "black", "center", "Old English Text MT")
if (event.x > this.canvas.getCenter().X - xBox + (index * 130) && event.x < this.canvas.getCenter().X - xBox + 100 + (index * 130)) {
if (event.y > this.canvas.getCenter().Y - yBox && event.y < this.canvas.getCenter().Y - yBox + 100) {
if (this.player.getScore() >= 10) {
this.canvas.clearArea(this.canvas.getCenter().X + 250, this.canvas.getCenter().Y - 140, 400, 100)
this.canvas.clearArea(this.canvas.getCenter().X - 550, this.canvas.getCenter().Y - 300, 300, 100)
if (this.hints < 3) {
this.player.scoreMinus(10)
this.hints++
}
this.canvas.writeTextToCanvas(`You have ${this.hints} hints`, 50, this.canvas.getCenter().X + 450, this.canvas.getCenter().Y - 100, "black", "center", "Old English Text MT")
this.canvas.writeTextToCanvas(`${this.player.getScore()} points`, 50, this.canvas.getCenter().X - 410, this.canvas.getCenter().Y - 250, "black", "center", "Old English Text MT")
console.log(this.hints)
}
}
}
}
}
}
private removeShopListener() {
document.getElementById('canvas').removeEventListener("click", this.hintShopListener())
}
我添加了2个“ let number1 / 2”,因为我认为它可以解决这个问题,但仍然使它们无效。我已经搜索了很多答案,但找不到很多朋友,我也遇到了同样的问题。 另外,对不起,如果我的代码没有任何意义,但是我只做了几个月,却没有任何经验。
在此先感谢您的帮助。