2012-07-15 23 views
0

再次即時通訊問題與我的畫布。我目前正在編寫一個Javascript空間射擊遊戲。如何查看我在帆布上拍攝的子彈?

我創建了一個子彈類,並希望Spaceship能夠拍攝子彈。問題是:沒有錯誤,但也沒有子彈。我不完全確定他們是否沒有畫出來,或者我看不到他們。

如果你能幫助我,你真的幫了我很多:

相關的文件(main.js,held.js)我已經上傳到我的Git: https://github.com/nemoxdelight/First_Game

對不起德國的評論,但它使人們更方便,我有時:P

我認爲這個問題是在某處這裏的held.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; 
} 

回答

0
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 46if(laeuft = true){

+2

'if(laeuft = true)'看起來他的意思是'=='。 – 2012-07-15 18:42:11

+0

哇,謝謝!不,它的作品,我現在也看到我的子彈:)謝謝你也指出,如果錯誤,我現在糾正它。顯然,我有一個弱碼壞編碼。 – user1478242 2012-07-15 19:04:41

+0

@ user1478242:您應該停止在代碼中切換語言。選擇德語還是英語,否則你會經常問自己:「我稱這種方法爲'zeichnen'還是'draw'?這個對象叫做'kugel'還是'bullet'? – Zeta 2012-07-15 21:10:01