2

I am currently working on a tower defense project on gridworld (not sure if that is significant). I have a timer that calls a method (TowerPlacer) from my main class to receive user text input via scanner. However, after the first time through, the terminal window stops allowing me to input text despite the TowerPlacer timer running.

Also, for whatever reason the method seems to stop all the bugs from spawning as well. Despite the tower being spawned literally doing nothing.

Here is the method for TowerPlacer

public static void TowerPlacer ()
{
  System.out.println ("You have " + money + " gold");
  Scanner kbReader = new Scanner(System.in);
  System.out.println ("Would you like to place a defense?");
  System.out.println ("1. Yes");
  System.out.println ("2. No");
  answer = kbReader.nextInt();
  kbReader.nextLine();
  if (answer == 1) {
    System.out.println ("Which defense would you like to place?");
    System.out.println ("1. Study defense - 50 gold");
    System.out.println ("2. Pencil defense - 100 gold");
    System.out.println ("3. Pen defense - 150 gold");
    System.out.println ("4. Cram defense - 200 gold");
    Tanswer = kbReader.nextInt();
    kbReader.nextLine();
    System.out.println ("Defenses cannot be placed in the path of the critters.");
    System.out.println ("Defenses not placed in viable locations will end the game.");
    System.out.println ("What is the X-coordinate of the defense? (0 - 9)");
    xCoord = kbReader.nextInt();
    kbReader.nextLine();
    System.out.println ("What is the Y-coordinate of the defense? (0 - 9)");
    yCoord = kbReader.nextInt();
    kbReader.nextLine();
    if (Tanswer == 1) {
      Study defense1 = new Study();
      world.add(new Location(xCoord, yCoord), defense1);
      System.out.println ("Defense Placed");
    }
    if (Tanswer == 2) {
      Study defense2 = new Study();
      world.add(new Location(xCoord, yCoord), defense2);
      System.out.println ("Defense Placed");
    }
    if (Tanswer == 3) {
      Study defense3 = new Study();
      world.add(new Location(xCoord, yCoord), defense3);
      System.out.println ("Defense Placed");
    }
    if (Tanswer == 4) {
      Study defense4 = new Study();
      world.add(new Location(xCoord, yCoord), defense4);
      System.out.println ("Defense Placed");
    }
  }
  if (answer == 2) {
    System.out.println ("Construction cancelled");
  }
  if (answer != 1 && answer != 2) {
    System.out.println ("Error, answer out of bounds");
  }
  System.gc();
}

Here is the code for my timer caller

public static void main(String[] args)
{
  Timer timer = new Timer();
  TimerA a = new TimerA();
  timer.schedule(a,0,2000);
  TimerC t = new TimerC();
  timer.schedule(t,0,2000);
  world.show();
  System.out.println ("Please do not pause the game after starting.");
  TowerPlacer();
}

The timer itself

class TimerA extends TimerTask
{
  public static int times = 0;
  public void run()
  {
    times++;
    if (times%10 == 0) {
      BoxBugRunner.TowerPlacer();
    }
    if (times >= 1000000) {
      //Stop Timer.
      this.cancel();
    }
  }
}
George Ng
  • 23
  • 2
  • 2
    Why have you called `TowerPlacer()` in main itself if it is already been called by the `Timer` ? – Vishal K Jun 08 '13 at 06:36
  • I called the TowerPlacer() in the main when the game starts so that the player can place the tower right away - and then the timer continues calling it. – George Ng Jun 09 '13 at 22:04

1 Answers1

1

The mistake is in your TimerTask. What you need is:

class SayHello extends TimerTask {

    public void run()
    {
      System.out.println("done");
    }

 }

And then change the main method as follows:

public static void main(String[] args)
{
    int delay = 2000; // delay for 2 seconds
    int period = 500; // repeat every .5 seconds

    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new SayHello(), delay, period);
}
Chris
  • 5,584
  • 9
  • 40
  • 58
  • Hi! Thanks for your quick response! This fixed the bugs spawning, however the terminal window now doesn't receive any input at all (as opposed to before where it accepted input once before locking up). Sorry for the late reply, was busy at prom yesterday haha. – George Ng Jun 09 '13 at 22:09
  • I did a test with the timer and a kbReader separately out of gridworld, and I still get the problem where I can't type into the terminal to input for the scanner. – George Ng Jun 09 '13 at 23:55
  • Nevermind! I solved the terminal window problem with a really weird while loop thing. No matter what I did, the Terminal Window wouldn't let me type in it while I had it running with the timer, but I unlocked the window by having a while loop near infinitely outputting nothing... Don't see the logic behind it but it works! I appreciate your answer though, it solved my problem of the bugs not spawning when I spawned towers! – George Ng Jun 10 '13 at 01:21