Array of bullets

I have a PImage array "shootBullet" that plays the bullet animation. How do I create PVector array of separate bullets. Currently my player can shoot one bullet dependent on facing right or left with a keyPress = DOWN. If my player changes direction after bullet shot, bullet changes direction with character... As well, if keyPress again after shot, first bullet disappears to create new one. Tried following along with this example (http://forum.processing.org/two/discussion/1324/how-to-create-a-bullet-array-for-asteroids-game) but am having trouble with the nested type...

Any suggestions are appreciated~ Thanks!! - the sheep

class Bullet
{
  int X, Y;
  boolean bulletState = false; 
  boolean shootState = false; 
  boolean canShoot = true;
  int maxImagesBullet = 4;
  int imageIndexBullet =  0;
  int shootRight;
  int shootLeft;
  PImage[] shootBullet = new PImage[maxImagesBullet];

  void loadImages()     // Load BULLET images
  { 
    for (int i = imageIndexBullet; i < shootBullet.length; i++) 
    {
      shootBullet[i] = loadImage("ammoMan" + i + ".png");
    }
  }

  void display() 
  {
    if (shootState != true) 
    {
      bullet.bulletState = false;
    }
    if (shootState == true) 
    {
      canShoot = false;
      if (player.chillState == "right"|| player.dirState == "right") 
      {
        image(bullet.shootBullet[bullet.imageIndexBullet], bullet.X, bullet.Y);
        bullet.X += 10;
      }
      if (player.chillState == "left"|| player.dirState == "left") 
      {
        image(bullet.shootBullet[bullet.imageIndexBullet], bullet.X, bullet.Y);
        bullet.X -= 10;
      }
      if (bullet.X <=player.X+200 || bullet.X >= player.X-200)
      {
       bullet.canShoot = false; 
      }
      if (bullet.X >=player.X+200 || bullet.X <= player.X-200) 
      {
        bullet.shootState = false;
        bullet.bulletState = false;
        bullet.canShoot = true;
        bullet.X= -360;
        shootRight = 0;
        shootLeft = 0;
        points.ammo -=1;
      }
    } 
    if (points.ammo <= 0)
    {
      bullet.canShoot = false;
    } else if (points.ammo > 0)
    {
      bullet.canShoot = true;
    }
  }

  void shooting() {
    bulletState = true;
    if (imageIndexBullet <= 3) {
      imageIndexBullet += 1;
    }
    if (imageIndexBullet > 3) {
      imageIndexBullet = 0;
    }
  }
}

class Player
{
  int X = 0; //X-coordinates of player
  int Y = 450; //Y-coordinates of player
  String dirState = "chill"; //states: chill,left,right
  String chillState = "right"; //states: left,right
  int maxImagesWalkR = 4; //Total # of images loaded
  int imageIndexWalkR = 0; //Initial image loaded
  int maxImagesWalkL = 4;
  int imageIndexWalkL = 0; 

  PImage[] DemocratWalkR = new PImage[maxImagesWalkR];
  PImage[] DemocratWalkL = new PImage[maxImagesWalkL];

  void loadImages() { //Load all images for the sequences
    for (int i = 0; i < DemocratWalkR.length; i++) {
      DemocratWalkR[i] = loadImage("HilaryR_Walk" + i + ".gif");
    } 
    for (int j = 0; j < DemocratWalkL.length; j++) {
      DemocratWalkL[j] = loadImage("HilaryL_Walk" + j + ".gif");
    }
  }
  void display() {
if (level.imageIndexLevel >= 1 || level.flagY <= 100) {
      noTint();
      if (dirState == "chill") {
        if (chillState == "right") {
          image(DemocratWalkR[imageIndexWalkR], player.X, player.Y);
        }
        if (chillState == "left") {
          image(DemocratWalkL[imageIndexWalkL], player.X, player.Y);
        }
      }    
      if (dirState == "right") {
        image(DemocratWalkR[imageIndexWalkR], player.X, player.Y);
      } else if (dirState == "left") {
        image(DemocratWalkL[imageIndexWalkL], player.X, player.Y);
      }
    }
  void runRight()
  {
    dirState = "right";
    if (imageIndexWalkR <= 4) {
      imageIndexWalkR += 1;
      player.X+=10;
    }
    if (imageIndexWalkR == 4) 
    {
      imageIndexWalkR = 0;
    }
  }
  void runLeft()
  {
    dirState = "left";
    if (imageIndexWalkL <= 4) {
      imageIndexWalkL += 1;
      player.X-=10;
      if (player.X < 0) 
      {
        player.X = 0;
      }
    }
    if (imageIndexWalkL == 4) {
      imageIndexWalkL =0 ;
    }
  }    
  void chill() {
    if (chillState == "chill") {
      player.X -=0;
    }
  }
  void checkState() {
    if (dirState == "right") {
      runRight();
      chillState = "right";
    } 
    if (dirState == "left") {
      runLeft();
      chillState = "left";
    }          
if (bullet.shootState == true) {
      bullet.shooting();
    }
    dirState = "chill";
    chill();
  }
  void checkKeys() 
  {
    if (keyPressed && keyCode == RIGHT)player.dirState = "right";
    if (keyPressed && keyCode == LEFT)player.dirState ="left";
    if (keyPressed && keyCode == DOWN && bullet.canShoot == true) 
    {
      //      player.shootState = true;
      println("SHOOTING...*FIRE* " );
      bullet.shootState = true;
      bullet.Y = player.Y+10;
      bullet.X = player.X+15;
    }
  }
}
Tagged:

Answers

  • edited April 2015

    within the class bullet you ask the player direction

    • thus the bullet is still hanging on the player

    solution

    • in the moment of shooting, store the player direction in the bullet class and use this instead
  • as an aside, think about making the PImage arrays inside the classes static. that way you'll only have one copy per class, not one copy per bullet / player. or make the PImage arrays global (which is worse, from a design point of view, but easier from a thinking point of view)

Sign In or Register to comment.