title: Duplicating sprites given the base type in javafx
body: <p>I am making a game in javaFX right now. Essentially, a player knows what type of bullet it has (say SingleShot). All bullet types extend off a general Projectile class. In the player class, I have a method which needs to spawn a bullet if a button is pressed. How can I spawn a clone of the bullet given only the type it is? So if my type is SingleShot (stored as a Projectile field), how do I spawn a new SingleShot without having a bunch of if-else statements checking through every type? </p>
  
  <pre><code>public class Projectile extends Sprite{
  int damage;
  int fireRate;
  
  public Projectile(Pane outerPane, double x, double y, double dy, double dx){
      super(outerPane,x,y,dy,dx);
      damage = 1;
      fireRate = 10;
  }
  
  public int getFireRate(){
      return this.fireRate;
  }
  public void setFireRate(int fr){
      fireRate=fr;
  }
  public int getDamage(){
      return damage;
  }
  public void setDamage(int d){
      damage=d;
  }
  }
  </code></pre>
  
  <p>This is an example bullet, but there are many others. There may also be multiple shapes/rectangles in a given bullet class.</p>
  
  <pre><code>public class SingleShot extends Projectile{
  Rectangle bullet;
  public SingleShot(Pane outerPane, double x, double y,double dy, double dx){
      super(outerPane,x,y, dy, dx);
      bullet = new Rectangle(0,0,10,10);
      bullet.setFill(Color.BLUE);
      getChildren().add(bullet);
  }
  }
  </code></pre>
  
  <p>Here is the player class. I have removed most of the extraneous code but if you need it please ask.</p>
  
  <pre><code>public class Player extends Sprite{
  
  Rectangle plyr;
  AnimationTimer timer;
  boolean movingN,movingE,movingW,movingS;
  boolean shootingN,shootingE,shootingW,shootingS;
  
  Projectile bulletType;
  ArrayList&lt;Projectile&gt; bullets;
  
  
  
  public Player(Pane outerPane, double x, double y, double w, double h, double xv, double yv){
      bulletType = new SingleShot(this,0,0,1,1);
      bullets= new ArrayList&lt;Projectile&gt;();
  
  }
  
  private void shoot(){
          if(shootingN){
              System.out.println("North shot");
              //how to determine what shot to create? possibly have hard checker for type
              //maybe have a method which makes a copy for the projectile type, then feeds it to here and this only changes vx,vy,x,y, etc.
              Projectile shot = new SingleShot(this,plyr.getX()+0.5*plyr.getWidth(),plyr.getY(),0,-10);
              getChildren().add(shot);
              bullets.add(shot);
          }
  
  }
  </code></pre>