0

I built a simple "Ping-Pong" 2D game in Java.

The game uses a Swing timer that goes off every 5 milliseconds and triggers an actionPerformed method.

This method "moves" (every 5 milliseconds) items on the screen, by changing their x and y coordinates and repainting the screen.

About an hour ago, the game ran fine. I then took a break and got back to the computer about a minute ago.

Now, when I run the game, everything is much slower. (For example, the "ping pong ball" the flies around the screen doesn't move as fast as it previously had).

What could be the problem? (I didn't think providing code was necessary, because maybe this kind of problem isn't related to the code, since it didn't change before the problem started to occure. If it is please let me know).

EDIT: Some code:

In the constructor of the main object of the program:

        timer = new Timer(40,this);
    timer.start();

In the main object's actionPerformed:

    public void actionPerformed(ActionEvent e) {

    bPaddle.move();
    tPaddle.move();
    ball.move();
    checkCollision();
    checkInScreen();

    repaint();

}

Anything else from the program to add here?

Thanks.

Josh
  • 95
  • 1
  • 9
  • 3
    Do you really need to redraw at 200fps? – Oliver Charlesworth Dec 28 '13 at 21:14
  • You mean I need to change the 5 millisecond period to something else? – Josh Dec 28 '13 at 21:15
  • 1
    Perhaps it's not a problem with your code but your computer in general? Anything else slowing down? – takendarkk Dec 28 '13 at 21:15
  • try with 17ms like 60fps – nachokk Dec 28 '13 at 21:16
  • I didn't think providing code was necessary == then this question isn't answerable :-), voting to close too – mKorbel Dec 28 '13 at 21:17
  • Generally speaking, I think you'll find 25fps more than fast enough for what you are trying, somewhere around 40 milliseconds. Even 60fps is only roughly 16 milliseconds...It could also be a resource issue, for [example](http://stackoverflow.com/questions/14886232/swing-animation-running-extremely-slow/14902184#14902184), was able to go from rough 200 animate objects to 4500 through some simple resource management... – MadProgrammer Dec 28 '13 at 21:17
  • There are many possible problems that could cause performance issues. Without the code it is almost impossible to detect. Even with the code it could be very hard. – Viktor K. Dec 28 '13 at 21:18
  • Maybe the code that does the timer and the action may be useful in providing clues to the behaviour. You should post some code. – Vincent Ramdhanie Dec 28 '13 at 21:18
  • Perhaps 5 ms is too fast. But why should my computer have a problem with it? It's pretty strong and has nothing running in the background. Do 2D games that repaint the screen every 5 ms usually cause this kind of thing? – Josh Dec 28 '13 at 21:19

1 Answers1

1

You should try to seperate the recalculating and the redrawing. That way, your ping-pong balls will always (seem to) move the same speed, regardless of the frames-per-second you can achieve.

One thing you will want to do though, is time how long it takes you to calculate te new positions and subtract that amount of time from the time you wait between recalculations.

ljgw
  • 2,751
  • 1
  • 20
  • 39
  • Can't say I understand what you mean. Could you provide an example? Thanks – Josh Dec 28 '13 at 21:24
  • He could also recalculate in a separate thread, posting the results to a shared spot, from which they are popped at scheduled moments by the EDT. – Marko Topolnik Dec 28 '13 at 21:35
  • I just _know_ I saw a good tutorial explaining this some months ago, but I cannot seem to find it now. The basic idea is that you use one timer to update the screen regularly, and another thread to caculate the new positions constantly. To avoid chunky movement, you need to ensure that the calculations happen at a fixed rate, and because calculation might take time, you need to subtract that time from the delay betwee the calculations in the position-update thread. – ljgw Dec 28 '13 at 21:40
  • Having the locations as a function of time should make this a lot smoother for sure. Good to point this out, since it is the preferred way to handle these. – Obicere Dec 28 '13 at 21:49