Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (562)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Multiple Timers  (Read 3503 times)
0 Members and 1 Guest are viewing this topic.
Offline Hazzy

Senior Newbie





« Posted 2011-10-15 19:17:40 »

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.
1  
import javax.swing.Timer;

1  
2  
3  
4  
5  
6  
7  
8  
9  
        public Player(){
                timer = new Timer(150 , this);
      timer.start();
        }

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

JGO Coder


Medals: 5



« Reply #1 - Posted 2011-10-15 19:31:09 »

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

and u could have Timer timer2.

for example

1  
2  
3  
4  
5  
6  
Timer timer;
Timer timer2;

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


does that make sense?
Offline Hazzy

Senior Newbie





« Reply #2 - Posted 2011-10-15 19:37:39 »

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?
1  
2  
public void actionPerformed(ActionEvent arg0) {
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zoto

Senior Member


Medals: 4



« Reply #3 - Posted 2011-10-15 20:03:32 »

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.
Offline Hazzy

Senior Newbie





« Reply #4 - Posted 2011-10-15 20:12:32 »

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.
Offline theagentd
« Reply #5 - Posted 2011-10-15 21:33:38 »

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).

Myomyomyo.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #6 - Posted 2011-10-17 05:30:49 »

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

See my work:
OTC Software
Offline cylab

JGO Ninja


Medals: 49



« Reply #7 - Posted 2011-10-17 08:21:59 »

While using the gameloop for this kind of stuff is the Right Thing (TM), 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):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
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
   }
}

Mathias - I Know What [you] Did Last Summer!
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #8 - Posted 2011-10-17 15:03:14 »

Or just use ActionEvent.getSource()?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public void actionPerformed(ActionEvent e)
{
    if (e.getSource() == timer1)
    {
        doTimer1();
    }
    else if (e.getSource() == timer2)
    {
        doTimer2();
    }
}

See my work:
OTC Software
Offline cylab

JGO Ninja


Medals: 49



« Reply #9 - Posted 2011-10-17 18:00:17 »

Works too, but is sooooo 90s... Wink

Mathias - I Know What [you] Did Last Summer!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #10 - Posted 2011-10-17 18:05:39 »

haha, once again demonpants makes us all look silly hahha
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #11 - Posted 2011-10-18 16:13:29 »

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.

See my work:
OTC Software
Offline cylab

JGO Ninja


Medals: 49



« Reply #12 - Posted 2011-10-19 06:23:19 »

1  
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.

Mathias - I Know What [you] Did Last Summer!
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #13 - Posted 2011-10-20 21:04:09 »

1  
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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
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.

See my work:
OTC Software
Offline Hazzy

Senior Newbie





« Reply #14 - Posted 2011-10-21 00:00:18 »

Or just use ActionEvent.getSource()?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public void actionPerformed(ActionEvent e)
{
    if (e.getSource() == timer1)
    {
        doTimer1();
    }
    else if (e.getSource() == timer2)
    {
        doTimer2();
    }
}

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.
Offline cylab

JGO Ninja


Medals: 49



« Reply #15 - Posted 2011-10-21 13:31:16 »

Still doesn't explain why you need 15 different anonymous classes for 15 different objects that generate action events.

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...

It's cleaner

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

(...) you can standardize what you do with it like for example perform all actions on the main thread rather than the EDT.

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.

Mathias - I Know What [you] Did Last Summer!
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #16 - Posted 2011-10-24 17:14:11 »

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

See my work:
OTC Software
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (28 views)
2014-09-21 02:42:18

BurntPizza (18 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (27 views)
2014-09-20 20:14:06

BurntPizza (29 views)
2014-09-19 03:14:18

Dwinin (46 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56

mitcheeb (71 views)
2014-09-08 06:06:29
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!