0

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!

sumitz1212
  • 63
  • 4
  • 10
  • 1
    Why don't you just subclass Node/Region/Group/whatever, give that subclass whatever properties (health/etc) you like and instantiate them every time you need them? Also, you may want to check out my answer [here](http://stackoverflow.com/questions/29057870/in-javafx-how-do-i-move-a-sprite-across-the-screen). It covers what you need. – Roland Apr 22 '15 at 16:25
  • Well my group is superclass to Node turrets and mobs, think? So you're saying I should have a subclass of those Nodes that represent individual turrets/mobs, and subclass that again for health? I don't have a clear idea of how to subclass the nodes that I have. – sumitz1212 Apr 22 '15 at 16:32
  • 1
    Just check the answer in the link I posted. – Roland Apr 22 '15 at 16:37
  • Yes, that comment will help a lot. Thank you so much – sumitz1212 Apr 22 '15 at 17:02
  • 1
    How are you doing with it? In case you need help, please be specific. – Roland Apr 23 '15 at 11:14
  • Sorry for not responding back sooner. But thank you so much for your help! This tower defense assignment was just what I need to get a B in my class (79.5%). – sumitz1212 May 20 '15 at 16:49
  • If you want a more detailed version, check the answer I just posted. – Roland May 20 '15 at 17:05

1 Answers1

1

That's a little bit too much to be answered on StackOverflow. However, I toyed around with Tower Defense last weekend and wrote a blog post if you are interested. You can get the full source from there. It's just a prototype, but does what you ask in your question. You can place as many turrets as you like with that code. The turrets rotate towards the enemy, fire at them and health is displayed and reduced.

You may also be interested in the A* Algorithm which I need for the next step in the Tower Defense game. Currently the enemies move only from top to bottom. Of course they should find a path from start to end, even when you place towers right in front of them as they move.

Screenie:

enter image description here

and youtube link.

Roland
  • 18,114
  • 12
  • 62
  • 93