Allegro 5 jumping problem

I have just implemented jumping into my game, and for some reason is really weird.
If I remove line 156, I can move left and right but I can only jump up, but not jump up and forwards or backwards (which you need in a platformer). However, if I leave the line in, I can jump up and forwards or backwards. But I cannot move left or right when on the ground.
It's had me stumped for a day or two now and is really annoying me; I would really appreciate it if someone could tell me whats wrong.

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <allegro5/allegro.h>
#include <allegro5/allegro_native_dialog.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_acodec.h>
#include <cstdlib>
#include <ctime>
#include "objects.h"

#define ScreenWidth 800
#define ScreenHeight 600

//Prototpyes
void initPlayer(Player &player);

void initArrow(Arrow arrow[], int size);
void drawArrow(Arrow arrow[], int size);
void fireArrow(Arrow arrow[], int size, Player &player);
void updateArrow(Arrow arrow[], int size);

/*void initEnemy(Enemy enemy[], int size);
void drawEnemy(Enemy enemy[], int size);
void spawnEnemy(Enemy enemy[], int size);
void spawnEnemy2(Enemy enemy[], int size);
void updateEnemy(Enemy enemy[], int size);*/

bool collision(Player &player, int ex, int ey, int eWidth, int eHeight, int pWidth, int pHeight);

//Global Variables
enum direction {LEFT, RIGHT};
int dir = LEFT;
int arrowCount = 10;
const int NUM_ARROW = 10;
const int NUM_ENEMY = 10;
const int groundHeight = 545;
static bool fired = false;
bool down = false, left = false, right = false, up = false;

int main()
{
	srand(static_cast<unsigned int>(time(0)));
	
	ALLEGRO_DISPLAY *display;

	if(!al_init())
	{
		al_show_native_message_box(NULL, NULL, "Error", "Falied to initialize allegro", NULL, NULL);
		return -1;
	}
	
	display = al_create_display(ScreenWidth, ScreenHeight);

	if(!display)
	{
		al_show_native_message_box(NULL, NULL, "Error", "Falied to initialize the display", NULL, NULL);
		return -1;
	}

	Player player;
	Arrow arrow[NUM_ARROW];
	Enemy enemy[NUM_ENEMY];
	bool done = false, active = false, draw = true;
	bool jump = false;
	int sourceX = 32, sourceY = 0;
	const float FPS = 55.0;
	float jumpSpeed = 15;
	float velX = 0, velY = 0;

	const float gravity = 1;

	al_init_image_addon();
	al_init_primitives_addon();
	al_install_keyboard();
	al_init_font_addon();
	al_init_ttf_addon();

	initPlayer(player);
	initArrow(arrow, NUM_ARROW);

	ALLEGRO_KEYBOARD_STATE keystate;
	ALLEGRO_BITMAP *character = al_load_bitmap("spritesheet(Bow & left + right).png");
	ALLEGRO_BITMAP *background = al_load_bitmap("ayers rock.png");
	ALLEGRO_BITMAP *ground = al_load_bitmap("Ground.png");
	ALLEGRO_BITMAP *box = al_load_bitmap("box.png");
	ALLEGRO_FONT *font = al_load_font("JUNGBN__.TTF", 23, 0);
	ALLEGRO_TIMER *timer = al_create_timer(1.0/FPS);
	ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
	al_set_window_title(display, "Australian Outback");
	al_register_event_source(event_queue, al_get_keyboard_event_source());
	al_register_event_source(event_queue, al_get_timer_event_source(timer));
	al_register_event_source(event_queue, al_get_display_event_source(display));

	al_start_timer(timer);
	while(!done)
	{
		ALLEGRO_EVENT event;
		al_wait_for_event(event_queue, &event);
		al_get_keyboard_state(&keystate);
			
		if(al_key_down(&keystate, ALLEGRO_KEY_ESCAPE))
		{
			done = true;
		}
		else if(event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
		{
			done = true;
		}
		
		
		else if(event.type == ALLEGRO_EVENT_TIMER)
		{
			active = true;
			updateArrow(arrow, NUM_ARROW);
			
			if(al_key_down(&keystate, ALLEGRO_KEY_D))
			{
				velX = player.speed;
				dir = RIGHT;
				
			}
			else if(al_key_down(&keystate, ALLEGRO_KEY_A))
			{
				velX = -player.speed;
				dir = LEFT;
			}
			else 
			{
				velX = 0;
				active = false;
			}
			if(al_key_down(&keystate, ALLEGRO_KEY_W) && jump)
			{
				velY = -jumpSpeed;
				jump = false;
			}
			if(active)
			{
				sourceX += al_get_bitmap_width(character) / 3;
			}
			else 
			{
				sourceX = 32; 
			}
			if(sourceX >= al_get_bitmap_width(character))
			{
				sourceX = 0;
			}
				sourceY = dir;
					draw = true;
			if(!jump)
				velY += gravity;
			else
				velX = 0;
				player.x += velX;
				player.y += velY;
			
			jump = (player.y >= groundHeight);

			if(jump)
			{
				player.y = groundHeight;
			}

		}
		if(collision(player, 531, 535, 48, 32, 32, 32))
		{
			if(dir == LEFT)
			{
				player.x += player.speed;
			}
			else if(dir == RIGHT)
			{
				player.x -= player.speed;
			}
		}
		
		if(al_key_down(&keystate, ALLEGRO_KEY_SPACE))
		{
			fireArrow(arrow, NUM_ARROW, player);
		}
		else
		{
			fired = false;
		}
		


	if(draw && al_is_event_queue_empty(event_queue))
	{
		draw = false;
		al_draw_bitmap_region(character, sourceX, sourceY * al_get_bitmap_height(character) / 2, 32, 32, player.x, player.y, NULL);
		drawArrow(arrow,NUM_ARROW);
		al_draw_bitmap(box, 531, 535, NULL);
		al_draw_textf(font, al_map_rgb(252,209, 22), 10, 10, 0, "Arrows: %i", arrowCount);
		al_flip_display();
		al_draw_bitmap(background, 0,0, 0);
		al_draw_bitmap(ground, 0, 549, 0);
	}
  }

	al_destroy_bitmap(character);
	al_destroy_font(font);
	al_destroy_display(display);
	al_destroy_event_queue(event_queue);
	al_destroy_timer(timer);
	return 0;
}
void initPlayer(Player &player)
{
	player.x = 10;
	player.y = 545;
	player.ID = PLAYER;
	player.lives = 3;
	player.speed = 5;
	player.score = 0;
}

void initArrow(Arrow arrow[], int size)
{
	for(int i = 0; i < size; i++)
	{
		arrow[i].ID = ARROW;
		arrow[i].speed = 5.0;
		arrow[i].live = false;
	}
}
void drawArrow(Arrow arrow[], int size)
{
	for(int i = 0; i < size; i++)
	{
		ALLEGRO_BITMAP *bArrow = al_load_bitmap("Arrow.png");
		if(arrow[i].live)
		{
			al_draw_bitmap(bArrow, arrow[i].x, arrow[i].y, NULL);
		}
	}
}
void fireArrow(Arrow arrow[], int size, Player &player)
{
	for(int i = 0; i < size; i++)
	{
		if(!arrow[i].live && !fired)
		{
			--arrowCount;
			arrow[i].live = true;
			arrow[i].x = player.x + 17;
			arrow[i].y = player.y + 22;
			fired = true;

		}
	}
}
void updateArrow(Arrow arrow[], int size)
{
	for(int i = 0; i < size; i++)
	{
		if(arrow[i].live)
		{
			
				arrow[i].x += arrow[i].speed;
				if(arrow[i].x >=  - 5 && arrowCount >= size)
				{
					arrow[i].live = false;
				}
			}
		
	}
}
bool collision(Player &player, int ex, int ey, int eWidth, int eHeight, int pWidth, int pHeight)
{
	if((player.x > ex + eWidth) || (player.y > ey + eHeight) ||
		(ex > player.x + pHeight) || (ey > player.y + pHeight))
	{
		return false;
	}
	return true;
}

Objects.h
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
enum IDS {PLAYER, ARROW, ENEMY};

class Player
{
	public:
		int x;
		int y;
		int ID;
		int lives;
		int score;
		float speed;
		int boundx;
		int boundy;
	
};
class Arrow
{
	public:
		int ID;
		int x;
		int x2;
		int y;
		int y2;
		bool live;
		float speed;

};
class Enemy
{
	public:
		int ID;
		int x;
		int y;
		int live;
		float speed;
		int boundx;
		int boundy;
};
Your indentation around line 156 is all screwed up.

That said.. was line 156 supposed to be velY = 0; (velY instead of velX)? I see no reason to kill all X velocity if the player is jumping, which is what that line is doing.
Last edited on
Wooow... I am stupid, can't believe I didn't see that. Little things like that have sometimes been the source of my problems in an allegro program and its really annoying when I finally realize that. Big thanks anyway :D
Topic archived. No new replies allowed.