简体   繁体   English

如何查看我在画布上射击的子弹?

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

Again im having a Problem with my Canvas. 我的画布再次出现问题。 I'm currently programming a Javascript Space Shooter. 我目前正在编写Javascript Space Shooter。

I created a Bullet Class and want the Spaceship to be able to shoot Bullets. 我创建了子弹班,并希望太空飞船能够射击子弹。 The Problem is: There a no Errors but also, no Bullets. 问题是:没有错误,也没有项目符号。 I'm not totally sure if they are just not drawn or I just can't see them. 我不确定它们是否只是未绘制或我看不到它们。

If you could help me, you would really help me a lot: 如果您可以帮助我,那么您将对我有很大帮助:

The relevant Files (main.js, held.js) i've uploaded to my Git: https://github.com/nemoxdelight/First_Game 我已经将相关文件(main.js,holded.js)上传到了我的Git: https : //github.com/nemoxdelight/First_Game

Sorry for the German Comments, but it makes it a lot easier for me sometimes :P 对不起德国评论,但这有时对我来说很容易:P

I think the Problem is somewhere in here in the held.js: 我认为问题出在hold.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();
       }
   }
}

There is no field called held_x , at least not in the constructor of Kugel . Kugel的构造器中没有名为held_x字段。 Maybe you wanted to use something like 也许您想使用类似

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();
        }
    }
}

By the way, do you really mean if(laeuft = true){ in main.js line 46 ? 顺便说一句,您真的是说main.js line 46 if(laeuft = true){吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM