Tower Defense game:
public class Main extends Application {
.......
private Node turret6;
private int turretCount = 0;
@Override
public void start(Stage stage) throws Exception {
mob1Image = new Image(MOB1_IMAGE_LOC);
............
turretImage = new Image(TURRET_IMAGE_LOC);
mob1 = new ImageView(mob1Image);
............
turret1 = new ImageView(turretImage);
Group group = new Group(mob1, mob2, mob3, mob4, mob5, home);
moveMobTo(1 * W / 10, H / 2, mob1);
...............................
moveMobTo(5 * W / 10, H / 2, mob5);
moveHomeTo(W * 9 / 10, H * 1 / 2);
Scene scene = new Scene(group, W, H, Color.BLUE);
scene.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if (turretCount == 0) {
group.getChildren().add(turret1);
moveTurretTo( event.getSceneX(), event.getSceneY(), turret1);
turretCount = turretCount + 1;
score = score - 10;
}
else if (turretCount == 1) {}
.............................
else if (turretCount == 5) {} }
});
stage.setScene(scene);
stage.show();
goEast = true;
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long now) {
int dx = 0, dy = 0;
if (goEast) dx += 1;
moveMobBy(dx, dy, mob1);
........................
moveMobBy(dx, dy, mob5); }
}; timer.start();}
private void moveMobBy(int dx, int dy, Node mobs) {}
private void moveMobTo(double x, double y, Node mobs) {}
private void moveTurretTo(double x, double y, Node turret) {}
public static void main(String[] args) { launch(args); }
}
So in this class I have 6 Mob Nodes and 6 Turret Nodes, the Mobs are created and move east, and 6 Turrets you place with the mouse event. The problem of course is I don't want to be limited by writing group.getChildren.add(turret1), etc. 6 times for each Node I want placed. And I also want each Mob to have a health bar property associated with it, or at least a health number, and I see no way of doing that for a Node.
Is there a way to make copies of a node that each do their own thing? So that I have a single private Node turret, and I can place it however many times? And is there some way I can associate Nodes with say a class Mob that has health properties? I'm sure there is, but I just can't figure it out.
Thanks for any help you can provide!