在javafx

时间:2018-05-07 01:55:48

标签: javafx cloning

我正在使用javaFX制作游戏。基本上,玩家知道它有什么类型的子弹(比如SingleShot)。所有子弹类型都延伸到普通的子弹类。在播放器类中,我有一个方法,如果按下按钮需要生成一个子弹。如果只给出它的类型,我怎样才能产生子弹的克隆?因此,如果我的类型是SingleShot(存储为Projectile字段),如何在没有一堆if-else语句检查每种类型的情况下生成一个新的SingleShot?

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

这是一个示例项目,但还有很多其他项目。在给定的子弹类中可能还有多个形状/矩形。

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

这是玩家类。我删除了大部分无关的代码,但如果您需要,请询问。

public class Player extends Sprite{

Rectangle plyr;
AnimationTimer timer;
boolean movingN,movingE,movingW,movingS;
boolean shootingN,shootingE,shootingW,shootingS;

Projectile bulletType;
ArrayList<Projectile> 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<Projectile>();

}

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

}

0 个答案:

没有答案