Results 1 to 5 of 5
  1. #1
    killpoppop is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default Swing Timers Issue.

    Hi i've implemented a jump feature using a swing timer. However is jump is pressed multiple times it activated the swing timer each time and causes it to get stuck. I've tried using

    Java Code:
    jumpTimer.setCoalesce( false  )
    but am having no luck. Ideally i would just like it to ignore any jump button presses until the timer has finished.

    Any idea how this can be done??

    Thanks,
    Pop

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I'd make the Timer a class variable, not a local variable (you may already be doing this). Then in the action listener for the button check to see if the timer is null and if not, if it is running (the Timer API will tell you the method that will do this). I'd do this in an if block, and only if it isn't running, start the timer.

  3. #3
    killpoppop is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default

    Java Code:
    ActionListener jump = new ActionListener()
            {
            public void actionPerformed( ActionEvent event )
            {
    			test = jumpTimer.isRunning() ;
    			System.out.println( test ) ;
    			ytrans = ytrans + gravity  ; 
    			xtrans = xtrans + xjump ;
    
    			if( ytrans == 59 )
    			{
    				manUpdate( finalpos ) ;
    				jumpTimer.stop();
    				gravity = -6 ;
    			}
    			else
    			{	
    				manUpdate( jumppos ) ;
    				gravity = gravity + 0.5 ;
    			}
    			repaint() ;
            }
            } ;
            jumpTimer = new Timer( 20, jump ) ;
            jumpTimer.start() ;
    	}
    I've tried numerous loops with the boolean and am getting nowhere. Any suggestions?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    So I see that you're checking if the Timer is running, great, but nowhere do I see you using this information to prevent the timer from restarting.

    you need something along these lines in the button's action listener:
    Java Code:
    if (jumpTimer == null || !jumpTimer.isRunning())
    {
      // create and start a new Timer object, give it to jumpTimer, 
      // and start it.
    }

  5. #5
    killpoppop is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default

    Yeah it's cos i've tried a lot and none have worked. I think i have it thanks to your suggestion! =]
    Java Code:
    if (jumpTimer == null || !jumpTimer.isRunning())
    {
            jumpTimer = new Timer( 20, jump ) ;
            jumpTimer.start() ;	
    }

Similar Threads

  1. [SOLVED] Swing Timer issue
    By Doctor Cactus in forum New To Java
    Replies: 6
    Last Post: 03-03-2009, 01:25 PM
  2. Question on swing timers
    By Samgetsmoney in forum New To Java
    Replies: 5
    Last Post: 02-20-2009, 08:34 AM
  3. Replies: 0
    Last Post: 12-31-2008, 03:20 PM
  4. Java1.5 Swing Issue
    By naveenbk in forum AWT / Swing
    Replies: 1
    Last Post: 11-23-2008, 09:03 PM
  5. map javax.swing.text.Element to javax.swing.text.View
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 08:02 PM

Posting Permissions

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