我有一个函数可以推送一个新的构造函数类,但我只想让它在没有一个构造函数的情况下推送一个新的类.
现在,我的代码基本上是这样的:
class InterSpot {
constructor(game){
this.game = game;
this.x = 100;
this.speedX = 0;
this.markedForDeletion = false;
this.lives = 5;
this.score = this.lives;
}
update(){
this.x += this.speedX;
if (this.x + this.width < 0) this.markedForDeletion = true;
}
draw(context){
context.fillStyle = 'blue';
context.fillRect(this.x, this.y, this.width, this.height);
context.fillStyle = 'black';
context.font = '20px Helvetica';
context.fillText(this.lives, this.x, this.y);
}
}
class Intro extends InterSpot {
constructor(game){
super(game);
this.width = 228 * 0.2; // Largeur d'une frame de l'ennemi
this.height = 169 * 0.2; // Hauteur d'une frame de l'ennemi
this.y = 100;
//this.y = Math.random() * (this.game.height * 0.9 - this.height);
}
}
addInteractionSpot(){
if (!this.interactionSpot.includes(Intro)) {
this.interactionSpot.push(new Intro(this));
console.log(this.interactionSpot);
console.log(this.interactionSpot.includes(Intro));
}
}
我try 使用indexof、Includes和typeof,但都不起作用.我只想检测屏幕上是否已经有一个名为‘Intro’的构造函数类,这样如果按下一个按钮,它就会消失5秒,而不是重新出现.