Multiple Timers

How would I implement multiple timers into one class? The code below only shows one timer that I’m using but I wish to add another one. I did try earlier on but I never quite figured out how I would have a separate method for it.
Is it possible? Thanks.


import javax.swing.Timer;


        public Player(){
                timer = new Timer(150 , this);
		timer.start();
        }

        @Override
	public void actionPerformed(ActionEvent arg0) {
		//Timer Code
	}

well you have Timer timer, which is the one you used

and u could have Timer timer2.

for example


Timer timer;
Timer timer2;

//whenever u use them
timer.start();
timer2.start();

does that make sense?

Yeah but how I would tell it what method to run?
Like the first timer initiates this method but I created another timer and it just ran the same thing, how would I change that?


public void actionPerformed(ActionEvent arg0) {
}

A solution that tracked elapsed time using the game loops update method would probably be a much better way to do timers in a game.

If you want to use javax.swing.Timer anyway then don’t have Player implement ActionListener but instead make a separate class that does and then pass a new instance of it to your Timer instead.

Thanks I’ll just go with managing it in the game loop instead.

Having multiple timers is very unreliable, as they may easily go out of sync. At best, some things might go slightly slower than other things. At worst, it crashes your game because something happened in an unexpected order. Go with a game loop and have it call player.update(), along with all other update methods (NPCs, bullets, animations, whatever you have in the game).

Yes, just store startTime and after X time passes do whatever you wanted to do with each timer.

While using the gameloop for this kind of stuff is the Right Thing ™, I just want to answer the question for other circumstances this is needed (like in Swing) - you can bind methods to Listeners with “anonymous inner classes” (since there are no closures or lambdas yet - Java 8 might change this):


class MyFoo
{
    MyFoo()
    {
         whateverIsHavingAListenerX.addActionListener(new ActionListener()
         {
              public void actionPerformed(ActionEvent event) { handleEventX(event); }
         });

         whateverIsHavingAListenerY.addActionListener(new ActionListener()
         {
              public void actionPerformed(ActionEvent event) { handleEventY(event); }
         });
    }

    void handleEventX(ActionEvent event)
    {
         // Do something for whateverIsHavingAListenerX
    }

    void handleEventY(ActionEvent event)
    {
         // Do something for whateverIsHavingAListenerY
    }
}

Or just use ActionEvent.getSource()?



public void actionPerformed(ActionEvent e)
{
    if (e.getSource() == timer1)
    {
        doTimer1();
    }
    else if (e.getSource() == timer2)
    {
        doTimer2();
    }
}

Works too, but is sooooo 90s… :wink:

haha, once again demonpants makes us all look silly hahha

Don’t worry, this is not usually the case.

A lot of people like to use multiple ActionListener’s, I never understood why personally. I had professors teaching it this way in school, even when you have like 15 different things that can throw ActionEvent’s (in a Swing interface).

I’d say do whatever is comfortable.


A lot of people like to use multiple ActionListener's, I never understood why personally.

Using anonymous inner classes is just one way to avoid implementing the Listener interface in the class that should receive the events, since it is not always desirable to clutter the api of your class with public Listener callbacks, since they are internal implementation details and should not be able to get called directly.

Yes, so:


public class MyClass
{
    public void MakeStuff()
    {
        MyListener l = new MyListener();
        whatever.addActionListener(l);
        otherwise.addActionListener(l);
    }

    private class MyListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if (e.getSource() == whatever)
            {
                doWhatever();
            }
            else if (e.getSource() == otherwise)
            {
                doOtherwise();
            }
        }
    }
}

Still doesn’t explain why you need 15 different anonymous classes for 15 different objects that generate action events. Just make one handler. It’s cleaner, and you can standardize what you do with it like for example perform all actions on the main thread rather than the EDT.

That has actually simplified things so much for me, thanks!
However, I’ve already migrated to the game loop, but knowing that this method exists will make things hell of a lot easier in the future.

I simply find it easier to read (and even shorter to write), especially when you have more than just ActionListeners. With your approach you will end up with a lot of different dispatcher methods with a lot of if/else blocks. If you like that, fine…

This is a bit of a stretch. Just your personal taste…

If that’s a requirement, you can do it this way, but you could do it my way, too. Make an OffThreadActionListener adapter class and make an anonymous instance of that.

True, I suppose. I guess it comes down to taste.