没有移动对象数组?

时间:2013-06-05 12:16:33

标签: javascript html5 canvas

我一直在制作一个游戏,我的问题是当你点击太空键时它射出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 个答案:

答案 0 :(得分:0)

好的,我想我想出来了

尝试在关键事件中添加isShooting false

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

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

}