SFLM input problem

I am writing Pong using SFML and am having a small problem with the input. When I press "W" which makes the paddle go up, it goes up 10 pixels then delays for like half a second and then goes up smoothly. If possible I would like to get rid of the delay. Below is the code that is causing this problem:

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
26
27
28
29
30
while(win.IsOpened( ))
	{

	        while(win.GetEvent(event))
		{

			switch(event.Type)
			{

				case (sf::Event::Closed):
					win.Close();
					break;

					//---------- W ----------

				case (event.KeyPressed):
					if(event.Key.Code == sf::Key::W)
					{
						//move paddle up
					}

					//---------- S ----------

					if(event.Key.Code == sf::Key::S)
					{
						//move paddle down
					}
					break;
			}
		}


I am trying to change that to something like the code below to make the movement more smooth when a key is held down. The only problem is this does not work. Can anyone point me in the right direction?


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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

bool w = false;
bool s = false;

while(win.IsOpened( ))
	{

		while(win.GetEvent(event))
		{

			switch(event.Type)
			{

				case (sf::Event::Closed):
					win.Close();
					break;

					//---------- W ----------

				case (event.KeyPressed):
					if(event.Key.Code == sf::Key::W)
					{
						w = true;
					}

					//---------- S ----------

					if(event.Key.Code == sf::Key::S)
					{
						s = true;
					}
					break;


				case (event.KeyReleased):
					if(event.Key.Code == sf::Key::S)
					{
						s = false;
					}

					if(event.Key.Code == sf::Key::W)
					{
						w = false;
					}
					break;		
			}
		}


Thanks,
Nick
You have the right idea with setting your 'w' and 's' variables to true/false.

All you have to do after that is move the paddle based on the state of those vars. You should check them once per frame -- the same time you move the ball and do other game logic.
sorry, I actually had something like that but it wasn't working. I figured it out now though.
Thanks,
Nick
Topic archived. No new replies allowed.