|
[A5] Anyone have problems with multiple keys being held down? |
Chris Katko
Member #1,881
January 2002
|
I thought at first maybe the WASD keys on my keyboard were conflicting, but I changed my code to arrow keys and I'm still having problems. My control code is based on online examples. 1
2void execute()
3 {
4 bool done = false;
5 do{
6 ALLEGRO_EVENT event;
7
8 al_wait_for_event(g::event_queue, &event);
9
10 if(event.type == ALLEGRO_EVENT_TIMER)
11 {
12 if(event.timer.source == g::timer_fps)
13 {
14 g::frame_rate = g::frames_passed;
15 g::frames_passed = 0;
16 }
17
18 //LOGIC
19 if(event.timer.source == g::timer)
20 {
21 g::vehicles[0].logic();
22 g::vehicles[1].logic();
23
24 }
25
26
27 }
28
29
30 if(event.type == ALLEGRO_EVENT_KEY_DOWN)
31 {
32 switch(event.keyboard.keycode)
33 {
34 case ALLEGRO_KEY_ESCAPE:
35 printf("Game done. \n");
36 done = true;
37 break;
38
39 case ALLEGRO_KEY_UP:
40 g::vehicles[0].set_rel_velocity(+g::VEHICLE_ACCEL);
41 break;
42 case ALLEGRO_KEY_DOWN:
43 g::vehicles[0].set_rel_velocity(-g::VEHICLE_ACCEL);
44 break;
45
46 case ALLEGRO_KEY_SPACE: //Slow to stop, never reverse.
47 g::vehicles[0].apply_brakes(g::VEHICLE_ACCEL*2);
48 break;
49
50
51 case ALLEGRO_KEY_LEFT:
52 g::vehicles[0].set_rel_angle(-g::VEHICLE_ANGLE_ACCEL);
53 break;
54 case ALLEGRO_KEY_RIGHT:
55 g::vehicles[0].set_rel_angle(+g::VEHICLE_ANGLE_ACCEL);
56 break;
57
58 }
59 }
60
61 if(al_is_event_queue_empty(g::event_queue))
62 {
63 al_clear_to_color(al_map_rgb(0,0,0));
64 //DRAW JUNK
65 al_flip_display();
66 g::frames_passed++;
67 }
68
69
70
71
72 }while(!done);
73 }
At first I had the drawing in with the logic timer, but I noticed an example that added the al_is_event_queue_empty() so I tried that, but the issue is still there. I can't turn and accelerate at the same time. When I press a second key, the first will stop pretty quickly. -----sig: |
Kris Asick
Member #1,424
July 2001
|
When using Allegro's event system for keyboard controls, you have to keep in mind that ALLEGRO_EVENT_KEY_DOWN only occurs ONCE for each time you newly press down a key. You must then consider that key to be held down until you receive an ALLEGRO_EVENT_KEY_UP event, at which point you stop processing whatever that key was doing. An alternative approach (and what I do) is to store Allegro's entire keyboard state into an ALLEGRO_KEYBOARD_STATE object using al_get_keyboard_state() once per frame, then simply check that object for each key you wish to detect as being held down. This is good for controls which players are expected to hold down, but you may still wish to use Allegro's event system for keys which only have an immediate effect for the frame they when pressed and no continual effect while held. --- Kris Asick (Gemini) |
|