如何查看项目符号I'我在画布上拍摄

How to see the Bullets I'm shooting on Canvas?

本文关键字:项目 何查看 符号      更新时间:2023-09-26

我的画布再次出现问题。我目前正在编写一个Javascript Space Shooter。

我创建了一个子弹类,希望太空船能够发射子弹。问题是:没有错误,但也没有项目符号。我不完全确定它们是没有画出来还是我看不见它们。

如果你能帮助我,你真的会帮我很多:

我上传到Git的相关文件(main.js,holded.js):https://github.com/nemoxdelight/First_Game

很抱歉收到德国人的评论,但这有时会让我更容易:p

我认为问题就在这里。js:

//Kugeln
this.leerTaste = false;
this.isShooting = false;
this.kugeln = [];
this.aktuelleKugel = 0;
//Wenn wir einen neuen Held erstellen, werden 20 Schuss "geladen"
for (var i = 0; i < 20; i++) {
    this.kugeln[this.kugeln.length] = new Kugel();
}
}
//Held Objekt
Held.prototype.draw = function() {
    this.tasteCheck();
    this.held_nase_x = this.held_x + 10;
    this.held_nase_y = this.held_y;
    this.schussCheck();
    this.kugeln_zeichnen();
    ctx.beginPath();
    ctx.fillStyle = this.farbe_ball;
    ctx.arc(this.held_x, this.held_y, this.groese_ball, 0, Math.PI * 2, true);
    ctx.arc(this.held_x - 17, this.held_y, this.groese_ball - 3, 0, Math.PI * 2, true);
    ctx.shadowBlur = this.blur_ball;
    ctx.shadowColor = this.farbe_blur_ball;
    ctx.closePath();
    ctx.fill();
    ctx.shadowColor = "transparent";
};
Held.prototype.tasteCheck = function() {
    if (this.hochTaste == true) {
        this.held_y -= this.speed;
    }
    if (this.rechtsTaste == true) {
        this.held_x += this.speed;
    }
    if (this.linksTaste == true) {
        this.held_x -= this.speed;
    }
    if (this.runterTaste == true) {
        this.held_y += this.speed;
    }
};
//Da unsere Kugeln nur in x-Richtung vom Held aus fliegen, sind sie alle größer als 0.          Der Ursprung findet sich beim Held.
Held.prototype.kugeln_zeichnen = function() {
    for (var i = 0; i < this.kugeln.length; i++) {
        if (this.kugeln[i].held_x >= 0) {
            this.kugeln[i].draw();
        }
    }
}
Held.prototype.schussCheck = function() {
    if (this.leerTaste && !this.isShooting) {
        this.isShooting = true;
        this.kugeln[this.aktuelleKugel].geschossen(this.held_nase_x, this.held_nase_y);
        this.aktuelleKugel++;
        //Wiederverwendne der Kugeln
        if (this.aktuelleKugel >= this.kugeln.length) {
            this.aktuelleKugel = 0;
        }
    } else if (!this.leerTaste) {
        this.isShooting = false;
    }
}
//KUGEL FUNKTIONEN
function Kugel() {
    this.groese_kugel = 3;
    //Kugeln aufbewahren
    this.drawX = -20;
    this.drawY = 0;
}
Kugel.prototype.draw = function() {
    this.drawX += 3;
    ctx.beginPath();
    ctx.fillStyle = '#ff99ff';
    ctx.arc(this.drawX, this.drawY, this.groese_kugel, 0, Math.PI * 2, true);
    ctx.closePath();
    ctx.fill();
    if (this.drawX > width) {
        this.drawX = -20;
    }
};
Kugel.prototype.geschossen = function(startX, startY) {
    //Sagt an was erlaubt ist.
    this.drawX = startX;
    this.drawY = startY;
}
Held.prototype.kugeln_zeichnen = function () {
for (var i = 0; i < this.kugeln.length; i++){
    if(this.kugeln[i].held_x >= 0) {
        this.kugeln[i].draw();
       }
   }
}

没有名为held_x的字段,至少在Kugel的构造函数中没有。也许你想用之类的东西

Held.prototype.kugeln_zeichnen = function () {
    for (var i = 0; i < this.kugeln.length; i++){   
        if(this.kugeln[i].drawX - this.held_nase_x > -20){
            this.kugeln[i].draw();
        }
    }
}

顺便问一下,你真的是指main.js line 46中的if(laeuft = true){吗?