Barney
Barney

Reputation: 31

Not moving through array of Objects?

I have been making a game, my problem is when you click the space key it shoots 1 bullet, but when you do it again nothing happens. I have made it so the game starts with 30 bullets, and they are stored at the top left of screen out of view. When space is clicked they get fired from the tip of your ship using its X, Y values.

Click here to see what I mean: http://www.taffatech.com/DarkOrbit.html - as you can see only 1 fires, ever.

Here is the bullet object

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;

}

This is the player Object: (the ship)

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

     }
}

This is in a method that checks what keys are down:

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

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

}

This is in a method that checks what keys are up:

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

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

}

Any other info you need just ask!

Upvotes: 0

Views: 62

Answers (1)

Smeegs
Smeegs

Reputation: 9224

Okay, I think I figured it out

try adding isShooting false to the key up event

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

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

}

Upvotes: 0

Related Questions