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

}

您需要的任何其他信息只需询问!

4

1 回答 1

0

好吧,我想我明白了

尝试将 isShooting false 添加到按键事件

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

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

}
于 2013-06-05T12:45:07.097 回答