|
Movement doesnt go on when holding button |
PamperBoy
Member #15,477
January 2014
|
Hi im kinda new to C++ and Allegro. And im using tutorials. But i have a problem. I Made a player class where i want a function (controls) to call in main. somehow i dont get it to work, so i put the movement in main. somehow that works but, it only moves once after pressing the button. It doesnt continue. Can someone help me with this? showing the main.cpp, InputManager class and Player class. 1#include<iostream>
2#include<allegro5\allegro5.h>
3#include<fstream>
4#include"Map.h"
5#include"Global.h"
6#include"Player.h"
7#include"InputManager.h"
8#include<allegro5\keyboard.h>
9
10using namespace std;
11
12int main()
13{
14 ALLEGRO_DISPLAY *display;
15
16 const float FPS = 60.0;
17 bool done = false;
18
19 //initialized. If not you get error message.
20
21 //initialized. If not you get error message.
22
23 if(!al_init())
24 { al_show_native_message_box(NULL, "Error", "Error",
25 "Cannot initialize Allegro", NULL, NULL);
26 return -1;
27 }
28
29 display = al_create_display(ScreenWidth, ScreenHeight);
30
31 if(!display)
32 { al_show_native_message_box(NULL, "Error", "Error",
33 "Cannot create display", NULL, NULL);
34 return -1;
35 }
36
37 //al_set_window_position(display, 200, 200);
38
39 al_init_primitives_addon();
40 al_install_keyboard();
41
42 ALLEGRO_TIMER *timer = al_create_timer(1.0 / FPS);
43 ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
44 ALLEGRO_KEYBOARD_STATE keyState;
45
46 al_register_event_source(event_queue, al_get_keyboard_event_source());
47 al_register_event_source(event_queue, al_get_timer_event_source(timer));
48 al_register_event_source(event_queue, al_get_display_event_source(display));
49
50 InputManager input;
51 Player player;
52 Map map;
53
54 map.Init();
55 player.Init();
56 //player.Draw(display);
57
58 al_start_timer(timer);
59
60 while(!done)
61 {
62 ALLEGRO_EVENT ev;
63 al_wait_for_event(event_queue, &ev);
64 al_get_keyboard_state(&keyState);
65
66
67 if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
68 {
69 done = true;
70 }
71
72 else if(input.IsKeyPressed(ev, ALLEGRO_KEY_ESCAPE))
73 {
74 done = true;
75 }
76
77 else if(input.IsKeyPressed(ev, ALLEGRO_KEY_RIGHT))
78 player.velx = player.speed;
79
80 else if(input.IsKeyPressed(ev, ALLEGRO_KEY_LEFT))
81 player.velx = -player.speed;
82
83 else if(input.IsKeyPressed(ev, ALLEGRO_KEY_DOWN))
84 player.vely = player.speed;
85
86 else if(input.IsKeyPressed(ev, ALLEGRO_KEY_UP))
87 player.vely = -player.speed;
88
89 else
90 {
91 player.velx = 0;
92 player.vely = 0;
93 }
94
95 player.x += player.velx;
96 player.y += player.vely;
97
98 map.Draw(display);
99 player.Draw(display);
100
101 }
102
103 al_destroy_display(display);
104 al_destroy_timer(timer);
105 al_destroy_event_queue(event_queue);
106
107 return 0;
108}
1#include "InputManager.h"
2
3
4InputManager::InputManager(void)
5{
6}
7
8
9InputManager::~InputManager(void)
10{
11}
12
13bool InputManager::IsKeyPressed(ALLEGRO_EVENT ev, int key)
14{
15 if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
16 {
17 if(ev.keyboard.keycode == key)
18 return true;
19 }
20 return false;
21}
22
23bool InputManager::IsKeyPressed(ALLEGRO_EVENT ev, std::vector<int> keys)
24{
25 if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
26 {
27 for(int i = 0; i < keys.size(); i++)
28 {
29 if(ev.keyboard.keycode == keys[i])
30 return true;
31 }
32 }
33 return false;
34}
35
36bool InputManager::IsKeyReleased(ALLEGRO_EVENT ev, int key)
37{
38 if(ev.type == ALLEGRO_EVENT_KEY_UP)
39 {
40 if(ev.keyboard.keycode == key)
41 return true;
42 }
43 return false;
44}
45
46bool InputManager::IsKeyReleased(ALLEGRO_EVENT ev, std::vector<int> keys)
47{
48 if(ev.type == ALLEGRO_EVENT_KEY_UP)
49 {
50 for(int i = 0; i < keys.size(); i++)
51 {
52 if(ev.keyboard.keycode == keys[i])
53 return true;
54 }
55 }
56 return false;
57}
1#include "Player.h"
2
3
4
5
6Player::Player(void)
7{
8}
9
10
11Player::~Player(void)
12{
13}
14
15void Player::Init()
16{
17 x = 50;
18 y = 50;
19 velx = 0;
20 vely = 0;
21 speed = 2;
22}
23
24void Player::Update()
25{
26 //Player::Controls();
27}
28
29void Player::Draw(ALLEGRO_DISPLAY *display)
30{
31 al_draw_circle(x + 10, y + 10, 5, al_map_rgb(255, 200, 0), 2.0);
32 al_flip_display();
33
34}
35
36/*void Player::Controls()
37{
38 if(input.IsKeyPressed(ev, ALLEGRO_KEY_RIGHT))
39 velx = speed;
40
41 else if(input.IsKeyPressed(ev, ALLEGRO_KEY_LEFT))
42 vely = -speed;
43
44 else
45 velx = 0;
46
47 x += velx;
48 y += vely;
49}*/
|
Hyllis
Member #15,521
February 2014
|
Hello, IsKeyPressed (ALLEGRO_EVENT_KEY_DOWN) only trigger once, when you press the key. Maybe there is another solution? which would be useful for me, too. But I don't think so. |
Dizzy Egg
Member #10,824
March 2009
|
Yeah, what Hyllis said. So, you need to set a flag when the key_down event happens, and then reset it when the key_up event happens; here is some psuedo code: 1
2bool key_left_down;
3
4if(key_down.key_left)
5{
6 key_left_down = true;
7}
8
9if(key_up.key_left)
10{
11 key_left_down = false;
12}
13
14if(key_left_down)
15{
16 position = position - 1;
17}
---------------------------------------------------- |
|