ok im a bit tired so sorry if this makes even less sense. the aim is to make
a game similar to break out. i have an applet which implements Runnable,
then when its initialised (in init() ) has:
Thread pong = new Thread(this)
pong.start();
the applet uses double buffering, and i am using it as a thread so i can
control how often the screen is updated like this:
public void run() {
requestFocus();
while (pong == Thread.currentThread() && currentLevel.play()) {
repaint();
try { Thread.sleep(19); } catch (InterruptedException e) {
System.out.println(e.getMessage()); }
}
}
if there is a better way of doing this (and there propably is) someone
please tell me. Ill note here that currentLevel.play() updates the position
of the balls on the screen etc and returns true if there are still balls in
play. And that i am also using a thread to control how often a games cycle
(recalculation of ball pos' etc) is. although im thinking of making the
level a thread itself and doing it that way (some one please tell me if this
is advisable)
So thats the background and the problem is that because im putting the
thread to sleep the keyDown function only detects a key when its awake (i
think) so the paddle jerks along slowly, rather than smoothly
Please note that this has been done on my own - and i have little experience
with OOP, so there is likely to be a lot of bad (ok terrible) design and i
would appreciate any advice / corrections / flaming :)
Linus
> ok im a bit tired so sorry if this makes even less sense. the aim is to make
> a game similar to break out. i have an applet which implements Runnable,
> then when its initialised (in init() ) has:
>
> Thread pong = new Thread(this)
> pong.start();
Okay. That will work, but it's very sloppy. Your applet is a window,
and it doesn't make sense for it to be "run". Instead, you could
replace this with:
Thread pong = new Thread(new Runnable() {
public void run()
{
// Your code here
}
});
pong.start();
And then later, if your game logic code becomes too complex, you can
move it to a separate named, top-level class instead. Either one is
better than shoving too many responsibilities on one class.
If possible, then using java.util.Timer to schedule screen updates is
probably a better idea that coordinating it with your own thread anyway,
but you'll still need a class, inner or otherwise, to act as a
TimerTask.
> So thats the background and the problem is that because im putting the
> thread to sleep the keyDown function only detects a key when its awake (i
> think) so the paddle jerks along slowly, rather than smoothly
First of all, I'd guess your problem with jerkiness is elsewhere. The
19-second sleep doesn't explain it. A 19-millisecond delay is too short
for the average human brain to notice. Even adjusting for clock
resolution on a typical system, this shouldn't be making anything appear
jerky.
Second of all, keyDown?!?!? That event model was deprecated with the
release of Java 1.1, back on the latter 90s. It's best to steer clear
of it, and use KeyListener instead.
> Please note that this has been done on my own - and i have little experience
> with OOP, so there is likely to be a lot of bad (ok terrible) design and i
> would appreciate any advice / corrections / flaming :)
You'll need to post code that demonstrates the problem first.

Signature
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Mikael Laine - 16 May 2004 16:25 GMT
I agree with what has been said before: your sleeping thread is not a
problem (in fact increasing the sleep time would only give the event
thread more time). To make gameplay smooth you need to have (boolean)
variables to hold if your keys are down or not. Then toggle these in
your keyDown/keyUP or KeyPressed/KeyReleased methods.
Roedy Green - 16 May 2004 18:42 GMT
>I agree with what has been said before: your sleeping thread is not a
>problem (in fact increasing the sleep time would only give the event
>thread more time)
Make sure you don't make the EVENT thread sleep though, or that has
exactly the opposite effect to the one you want. That's the #1 newbie
problem with Threads.
See http://mindprod.com/jgloss/thread.html

Signature
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.