Results 1 to 3 of 3
  1. #1
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default Java thread issue. booleans don't seem to work

    Here's a short version of one of my classes of my game. I took out all the code that doesn't have to do with my current bug.

    The bug is when I make the player fire, he is supposed to have to wait for 5000 milliseconds before he can fire again. I tested the code out, and he could fire as often as he wanted.


    I need some help finding the error in my code.

    What the code is supposed to do:

    Get the boolean notFiring from firethread when the player clicks the mouse, which starts as true.

    The if() in the mousePressed method should see that the value from notFiring is true.

    The code inside the if() will cause the player to shoot a bullet, and then set the boolean value of testFire to true through the isFiring() method of firethread.

    The if() in the firethread's run() method should see that testFire is now set to true, so it should begin by setting notFiring to false, sleeping for 5000 milliseconds, and then setting notFiring to true again and set testFire to false again.

    While the thread is sleeping, the player should be unable to shoot because firethread.notFiring() should return false, so the player shouldn't be able to fire until notFiring is set to true again at the end of the run() method of firethread.



    Java Code:
    import java.awt.event.KeyEvent;
    import java.awt.event.MouseEvent;
    import java.util.ArrayList;
    
    
    public class Player
    {
    
      public FireThread firethread;
    
      Player()
      {
        firethread = new FireThread();
        firethread.start();
      }
    
      public void fire()
      {
        //not needed code of how the firing works, just know it fires a bullet
      }
    
      public void mousePressed(MouseEvent e)
      {
        if(firethread.notFiring())
        {
          fire();
          firethread.isFiring();
        }
      }
    
    
      class FireThread extends Thread
      {
    
        public boolean notFiring;
        public boolean testFire;
    
        FireThread()
        {
          notFiring = true;
          testFire = false;
        }
    
        public void run()
        {
          while(true)
          {
    	if(testFire)
    	{
              notFiring = false;
              try
              {
                sleep(5000);
              }
              catch(InterruptedException e)
              {
    	    e.printStackTrace();
                System.exit(0);
              }
    	  notFiring = true;
    	  testFire = false;
            }
          }
        }
    
        public boolean notFiring()
        {
          return notFiring;
        }
    
        public void isFiring()
        {
          testFire = true;
        }
      }
    }

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    well, couldn't we achieve the "only fire if within 5 seconds" with a simple variable. e.g. "lastFiredTime"

    Java Code:
    Date lastFiredTime = null;
    public void mousePressed(MouseEvent e)
    {
      Date now = new Date();
      if (lastFiredTime == null || (now.getTime() - lastFiredTime.getTime()) > 5000) 
      {
          fire();
        lastFiredTime = now;
      }
    }
    Where I would make the 5000 be some member variable as well instead of the literal constant, in case I ever wanted to change this.
    And this approach doesn't even need a fire thread right.

  3. #3
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default

    Oh, yes, this does solve my problem completely. Thank you for providing a simple solution rather than delving into my thread issue. :D

Similar Threads

  1. Having problems with static booleans
    By Medieve in forum New To Java
    Replies: 4
    Last Post: 12-08-2010, 07:07 PM
  2. arrays with booleans
    By hoosierfan24 in forum New To Java
    Replies: 24
    Last Post: 10-04-2010, 02:07 AM
  3. How can a thread disorder in other thread work?
    By arefeh in forum New To Java
    Replies: 0
    Last Post: 01-02-2010, 04:16 PM
  4. Finding Strings, booleans and Integers
    By Pez in forum SWT / JFace
    Replies: 1
    Last Post: 07-19-2009, 03:24 PM
  5. Replies: 3
    Last Post: 05-06-2009, 01:11 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •