2013-06-05 2 views
0

Я делаю игру, моя проблема в том, когда вы нажимаете клавишу пробела, она стреляет в 1 пулю, но когда вы делаете это снова, ничего не происходит. Я сделал так, чтобы игра начиналась с 30 пуль, и они хранятся в верхнем левом углу экрана вне поля зрения. Когда нажимается пробел, они увольняются с кончика вашего корабля, используя его значения X, Y.Не перемещаться по массиву объектов?

Нажмите здесь, чтобы посмотреть, что я имею в виду: http://www.taffatech.com/DarkOrbit.html - как вы можете видеть только 1 пожар, когда-либо.

Вот пуля объект

function Bullet() //space weapon uses this 
{ 
this.srcX = 0; 
this.srcY = 1240; 
this.drawX = -20; 
this.drawY = 0; 
this.width = 11; 
this.height = 4; 
this.bulletSpeed = 3; 
this.bulletReset = -20; 
} 

Bullet.prototype.draw = function() 
{ 

this.drawX += this.bulletSpeed; 
ctxPlayer.drawImage(spriteImage,this.srcX,this.srcY,this.width,this.height,this.drawX,this.drawY,this.width,this.height); 

if (this.drawX > canvasWidth) 
    { 
    this.drawX = this.bulletReset; 

    } 

} 

Bullet.prototype.fire = function(startX, startY) 
{ 

    this.drawX = startX; 
    this.drawY = startY; 

} 

Это игрок объекта: (корабль)

function Player() //Object 
{ 

//////Your ships values 
this.PlayerHullMax = 1000; 
this.PlayerHull = 1000; 
this.PlayerShieldMax = 1000; 
this.PlayerShield = 347; 
this.SpaceCrystal = 2684; 
this.Speed = 5; //should be around 2 pixels every-time draw is called by interval, directly linked to the fps global variable 
//////////// 

///////////flags 
this.isUpKey = false; 
this.isDownKey = false; 
this.isLeftKey = false; 
this.isRightKey = false; 
////////space Weapon 
this.noseX = this.drawX + 100; 
this.noseY = this.drawY + 30; 

this.isSpaceBar = false; 
this.isShooting = false; 
this.bullets = []; 
this.currentBullet = 0; 
this.bulletAmount = 30; 

for(var i = 0; i < this.bulletAmount; i++) // 
    { 

    this.bullets[this.bullets.length] = new Bullet(); 

    } 
///////////// 

////Pick Ship 
this.type = "Cruiser"; 
this.srcX = PlayerSrcXPicker(this.type); 
this.srcY = PlayerSrcYPicker(this.type); 
this.drawX = PlayerdrawXPicker(this.type); 
this.drawY = PlayerdrawYPicker(this.type); 
this.playerWidth = PlayerWidthPicker(this.type); 
this.playerHeight = PlayerHeightPicker(this.type); 
//// 


} 

Player.prototype.draw = function() 
{ 
ClearPlayerCanvas(); 
ctxPlayer.globalAlpha=1; 
this.checkDirection(); //must before draw pic to canvas because you have new coords now from the click 

this.noseX = this.drawX + (this.playerWidth-10); 
this.noseY = this.drawY + (this.playerHeight/2); 
this.checkShooting(); 
this.drawAllBullets(); 



ctxPlayer.drawImage(spriteImage,this.srcX,this.srcY,this.playerWidth,this.playerHeight,this.drawX,this.drawY,this.playerWidth,this.playerHeight); 

}; 


Player.prototype.drawAllBullets = function() 
{ 

    for(var i = 0; i < this.bullets.length; i++) 
    { 
    if(this.bullets[i].drawX >= 0) 
    { 

     this.bullets[i].draw(); 

    } 

    } 
} 


Player.prototype.checkShooting = function() 
{ 

    if(this.isSpaceBar == true && this.isShooting == false) 
    { 

     this.isShooting = true; 
     this.bullets[this.currentBullet].fire(this.noseX, this.noseY); 
     this.currentBullet++; 

     if(this.currentBullet >= this.bullets.length) 
     { 
     this.currentBullet = 0; 
     } 


     else if(this.isSpaceBar == false) 
     { 

     this.isShooting = false; 
     } 

    } 
} 

Это в метод, который проверяет, какие клавиши вниз:

if (KeyID === 32) //spacebar 
{ 

Player1.isSpaceBar = true; 
e.preventDefault(); //webpage dont scroll when playing 

} 

Это метод, который проверяет, какие клавиши:

if (KeyID === 32) //left and a keyboard buttons 
{ 

Player1.isSpaceBar = false; 
e.preventDefault(); //webpage dont scroll when playing 

} 

Любая другая информация, которую вам нужно просто спросить!

+1

Может быть, вы должны попытаться отлаживать немного сузить, где проблема. Попробуйте выполнить регистрацию потока вашей программы и состояние переменных с помощью 'console.log'. Мы можем помочь вам легче, если нам не придется отлаживать всю вашу программу. – rednaw

+0

вы можете нажать F12 в хроме, чтобы открыть инструменты для разработчиков. У Firefox также есть встроенная версия, но я обычно устанавливаю Firebug, который открывается с F12, а также – HMR

+0

Я добавил несколько предупреждений, и они, похоже, работают, но кроме этого я потерял то, что может быть причиной:/ – Barney

ответ

0

Хорошо, я думаю, что я понял это

попробуйте добавить isShooting ложное под ключ до события

if (KeyID === 32) //left and a keyboard buttons 
{ 

Player1.isSpaceBar = false; 
Player1.isShooting = false; 
e.preventDefault(); //webpage dont scroll when playing 

} 
+0

Молодец, сэр, спасибо! Я просто отвечал на ваше последнее сообщение, когда сказал, что он был отредактирован :) Правильный ответ! ;) – Barney

Смежные вопросы