View Full Version : Pong Questions:
blobbo
2004.01.19, 11:34 PM
Hi all,
so my cocoa pong is going ok here. i've got a paddle that moves when i press up or down, the ball bounces off the walls ok. but i'm encountering some sticky animation questions that aren't easily answered. so here goes:
taking keyboard input, parsing it, and shooting off a message to my controller who in turn modifies data in my paddle model is great, easy to understand. but it bogs down my ball. i tried putting my ball animation within a thread, and it's better, but still a bit boggy. it just bogs through the animate method every 0.015 seconds and refreshes my view. it's ok, but when i move my paddle there's still (barely) noticeable lag.
so should i have two timers running, one that updates the ball, and one that updates the paddles? what's a good paradigm for this situation?
thanks in advance...
skyhawk
2004.01.20, 01:10 AM
K.I.S.S. is a great paradigm
blobbo
2004.01.20, 08:48 PM
*rolls eyes* that doesn't answer anything :) basically, how do i get my ball moving at a consistent speed while taking keyboard input?
NCarter
2004.01.20, 09:27 PM
I don't really know what's going on in your program, but from your description it sounds like your keyboard input is directly controlling the speed of the ball in some way. Is it being influenced by the keyboard repeat rate or something? If so, that's not a good idea in a realtime game.
I suggest you have a single timer which controls all game activity and redraws the screen (it's only Pong, no need to get sophisticated!). Every time the timer fires, you move the paddles and the ball, then redraw the screen. The keyboard events shouldn't move the paddles or update the screen directly - instead, when you receive a keyboard event, make a note of what it was and utilise that information the next time the timer fires.
blobbo
2004.01.21, 07:14 PM
well that's basically what's happening. you know, i think you're right about my key repeating thing. there's a delay between the first press and the first reaction. right now the system works just as you say, the view takes the keyboard input with the following code:
- (void)keyDown:(NSEvent *)theEvent
{
NSString *keyChar = [theEvent charactersIgnoringModifiers];
switch ([keyChar characterAtIndex:0])
{
case NSUpArrowFunctionKey:
[PongControllerOutlet movePaddle:1];
[self setNeedsDisplay:YES];
return;
case NSDownArrowFunctionKey:
[PongControllerOutlet movePaddle:0];
[self setNeedsDisplay:YES];
return;
}
}
and changes the paddle's state. oh wait. i just saw that. i don't need to redraw. d'oh! redundant post. ah well, i'll post it anyway to show how i can make silly mistakes! let me try that...
blobbo
2004.01.21, 09:08 PM
ok, so the extra redraws are gone and so are my jagged graphics problems! w00t. thanks a ton, that was very helpful.
now, on to the next problem: there's a noticeable "key repeat" lag for my mousedown event after the initial call. how do i set the key repeat rate to nothing for this app? thanks :)
NCarter
2004.01.21, 09:22 PM
Great, I'm pleased to hear that it's working better! :)
now, on to the next problem: there's a noticeable "key repeat" lag for my mousedown event after the initial call. how do i set the key repeat rate to nothing for this app?
That sounds wrong - I wouldn't expect mousedown events to be affected by the key repeat rate unless there's something odd going on with your program.
It might help if you briefly explained which events you handle and how you're using each kind of event.
skyhawk
2004.01.21, 10:45 PM
keydown is affected by key repeat rate.
BUT! if you capture keydown and key up, you can simulate no key repeat
blobbo
2004.01.21, 11:16 PM
oh i get it. so set a flag when i get a keydown, then take away the flag with keyup. i'll try that now.
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.