Pergunta

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();
    }
  }
}
Foi útil?

Solução

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);
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top