Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Having problems dont know what causes it.

This thread is locked; no one can reply to it. rss feed Print
Having problems dont know what causes it.
PamperBoy
Member #15,477
January 2014

Hi, I am trying to make a game and im using tutorials from CodingMadeEasy. Now, i got to the point of implementing Collision detection and gravity. i guess it works but, i keep falling and when i touch my platform i go back to the spawn point. Also when i press up (jump) it does the same. Right and left dont make me fall even faster:/ ive been trying multiple things but cant seem to find a solution.

Could someone help me out?

It may be a bit much to read but ill post all the .cpp files.

Main

#SelectExpand
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"Collision.h" 9#include<allegro5\keyboard.h> 10 11using namespace std; 12 13int main() 14{ 15 ALLEGRO_DISPLAY *display; 16 17 const float FPS = 60.0; 18 bool done = false; 19 20 //initialized. If not you get error message. 21 22 //initialized. If not you get error message. 23 24 if(!al_init()) 25 { al_show_native_message_box(NULL, "Error", "Error", 26 "Cannot initialize Allegro", NULL, NULL); 27 return -1; 28 } 29 30 display = al_create_display(ScreenWidth, ScreenHeight); 31 32 if(!display) 33 { al_show_native_message_box(NULL, "Error", "Error", 34 "Cannot create display", NULL, NULL); 35 return -1; 36 } 37 38 //al_set_window_position(display, 200, 200); 39 40 al_init_primitives_addon(); 41 al_install_keyboard(); 42 43 ALLEGRO_TIMER *timer = al_create_timer(1.0 / FPS); 44 ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); 45 ALLEGRO_KEYBOARD_STATE keyState; 46 47 al_register_event_source(event_queue, al_get_keyboard_event_source()); 48 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 49 al_register_event_source(event_queue, al_get_display_event_source(display)); 50 51 InputManager input; 52 Player player; 53 Map map; 54 Collision collision; 55 56 map.Init(); 57 player.Init(); 58 collision.Init(); 59 60 61 62 63 al_start_timer(timer); 64 65while(!done) 66 { 67 ALLEGRO_EVENT ev; 68 al_wait_for_event(event_queue, &ev); 69 al_get_keyboard_state(&keyState); 70 71 72 if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 73 { 74 done = true; 75 } 76 77 else if(input.IsKeyPressed(ev, ALLEGRO_KEY_ESCAPE)) 78 { 79 done = true; 80 } 81 82 player.Controls(ev, input); 83 map.Draw(display); 84 player.SetPosition(); 85 player.Draw(display); 86 collision.Update(display, player); 87 88 } 89 90 91 92 al_destroy_display(display); 93 al_destroy_timer(timer); 94 al_destroy_event_queue(event_queue); 95 96 return 0; 97}

Player

#SelectExpand
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 width = 10; 18 height = 10; 19 x = 100; 20 y = 450; 21 x2 = x + width; 22 y2 = y + height; 23 speed = 3; 24 jumpspeed = -15; 25 gravity = 3; 26 velx = vely = 0; 27 jump = platform = false; 28 hDir = 0; 29 vDir = 2; 30 31 32} 33 34void Player::Update(ALLEGRO_EVENT ev, InputManager &input) 35{ 36 Player::Controls(ev, input); 37} 38 39void Player::Draw(ALLEGRO_DISPLAY *display) 40{ 41 al_draw_circle(x + width, y + height, 5, al_map_rgb(255, 200, 0), 2.0); 42 al_flip_display(); 43 44} 45 46void Player::Controls(ALLEGRO_EVENT ev, InputManager &input) 47{ 48 if(input.IsKeyPressed(ev, ALLEGRO_KEY_RIGHT)) 49 { 50 velx = speed; 51 hDir = 1; 52 //vely = 0; 53 } 54 55 else if(input.IsKeyPressed(ev, ALLEGRO_KEY_LEFT)) 56 { 57 velx = -speed; 58 hDir = 2; 59 //vely = 0; 60 } 61 62 63 64 //else if(input.IsKeyPressed(ev, ALLEGRO_KEY_DOWN)) 65 //{ 66 // vely = speed; 67 // velx = 0; 68// } 69 70 //else 71 //velx = 0; 72 //vely = 0; 73 74 //velx = 0; 75// hDir = 0; 76 77 if(jump = true && input.IsKeyPressed(ev, ALLEGRO_KEY_UP)) 78 { 79 vely = jumpspeed; 80 platform = false; 81 jump = false; 82 //vDir = 1; 83 } 84} 85 86void Player::SetPosition() 87{ 88 if (vely >- 0) 89 { 90 vDir = 2; 91 } 92 93 if(platform == false) 94 { 95 vely += gravity; 96 } 97 98 else 99 { 100 vely = 0; 101 } 102 103 x =+ velx; 104 y =+ vely; 105 x2 = x + width; 106 y2 = y + height; 107}

Collision

#SelectExpand
1#include "Collision.h" 2 3 4Collision::Collision() 5{ 6} 7 8 9Collision::~Collision() 10{ 11} 12 13void Collision::Init() 14{ 15 loadCounterX = loadCounterY = 0; 16 Collision::LoadCollisionMap("ColMap1.txt"); 17} 18 19void Collision::Update(ALLEGRO_DISPLAY *display, Player &player) 20{ 21 Collision::PlatformCollision(display, player); 22} 23 24void Collision::Draw(ALLEGRO_DISPLAY *display) 25{ 26 27} 28 29void Collision::LoadCollisionMap(const char *filename) 30{ 31 ifstream openfile(filename); 32 if(openfile.is_open()) 33 { 34 openfile >> mapSizeX >> mapSizeY; 35 while(!openfile.eof()) 36 { 37 openfile >> ColMapFile[loadCounterX][loadCounterY]; 38 loadCounterX ++; 39 if (loadCounterX >= mapSizeX) 40 { 41 loadCounterX = 0; 42 loadCounterY ++; 43 } 44 } 45 loadCounterX = loadCounterY = 0; 46 } 47 else 48 { 49 { al_show_native_message_box(NULL, "Error", "Error", 50 "Cannot find collision map file", NULL, NULL); 51 } 52 53 } 54} 55 56void Collision::PlatformCollision(ALLEGRO_DISPLAY *display, Player &player) 57{ 58 for (int i = 0; i < mapSizeX; i++) 59 { 60 for(int j = 0; j < mapSizeY; j++) 61 { 62 if(ColMapFile[i][j] == 1) 63 { 64 if(player.x > i * blockSize + blockSize || player.y > j*blockSize +blockSize || player.x2 < i*blockSize || player.y2 < j*blockSize) 65 { 66 //no collision 67 player.platform = false; 68 } 69 70 else 71 { 72 if(player.vDir == 2 && player.y - player.vely <= j*blockSize) 73 { 74 player.y = j*blockSize - 10; 75 player.y2 = player.y + 10; 76 player.platform = true; 77 player.jump = true; 78 player.vely = 0; 79 } 80 else if (player.vDir == 1) 81 { 82 player.y += player.speed * 2; 83 player.y2 = player.y + 10; 84 player.vely = 0; 85 player.platform = false; 86 player.jump = false; 87 } 88 89 if (player.hDir == 1 && player.platform == false) 90 { 91 player.x -= player.speed; 92 player.x2 = player.x + 10; 93 } 94 95 else if (player.hDir == 2 && player.platform == false) 96 { 97 player.x += player.speed; 98 player.x2 = player.x + 10; 99 } 100 } 101 } 102 } 103 } 104}

InputManager

#SelectExpand
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}

Map

#SelectExpand
1#include "Map.h" 2 3 4Map::Map() 5{ 6} 7 8 9Map::~Map() 10{ 11} 12 13void Map::Init() 14{ 15 loadCounterX = loadCounterY = 0; 16 Map::LoadMap("Map1.txt"); 17} 18 19void Map::Update() 20{ 21 22} 23 24void Map::Draw(ALLEGRO_DISPLAY *display) 25{ 26 for(int i = 0; i < mapSizeX; i++) 27 { 28 for(int j = 0; j < mapSizeY; j++) 29 { 30 if(MapFile[i][j] == 0) 31 al_draw_filled_rectangle(i * blockSize, j * blockSize, i * blockSize + blockSize, j * blockSize + blockSize, al_map_rgb(0, 0, 0)); 32 33 else if(MapFile[i][j] == 1) 34 al_draw_filled_rectangle(i * blockSize, j * blockSize, i * blockSize + blockSize, j * blockSize + blockSize, al_map_rgb(255, 255, 255)); 35 36 37 else if(MapFile[i][j] == 2) 38 al_draw_filled_rectangle(i * blockSize, j * blockSize, i * blockSize + blockSize, j * blockSize + blockSize, al_map_rgb(0, 0, 0)); 39 40 //flag stick 41 for(int j = 0; j < mapSizeY; j++) 42 { 43 if(MapFile[i][j] == 2) 44 al_draw_line(i * blockSize + 10, j * blockSize, i * blockSize + blockSize - 10, j * blockSize + blockSize, al_map_rgb(255, 255, 255),1); 45 46 //flag 47 for(int j = 0; j < mapSizeY; j++) 48 { 49 if(MapFile[i][j] == 2) 50 al_draw_filled_triangle(i * blockSize, j * blockSize + 5, i * blockSize + 10 , j * blockSize, i * blockSize + 10, j * blockSize + 10, al_map_rgb(0, 220, 0)); 51 } 52 } 53 54 } 55 } 56 al_flip_display(); 57 58} 59 60void Map::LoadMap(const char *filename) 61{ 62 ifstream openfile(filename); 63 if(openfile.is_open()) 64 { 65 openfile >> mapSizeX >> mapSizeY; 66 while(!openfile.eof()) 67 { 68 openfile >> MapFile[loadCounterX][loadCounterY]; 69 loadCounterX++; 70 if(loadCounterX >= mapSizeX) 71 { 72 loadCounterX = 0; 73 loadCounterY ++; 74 } 75 } 76 loadCounterX = loadCounterY =0; 77 } 78 79 80}

#00JMP00
Member #14,740
November 2012

I sometimes jump, sometimes I don't,

but some people seem to jump all the time:

if(jump = true && input.IsKeyPressed(ev, ALLEGRO_KEY_UP))

Maybe you should try yoda.

PamperBoy
Member #15,477
January 2014

Well thx for referring me to yoda. But im pretty sure that is not causing the problem.

#00JMP00
Member #14,740
November 2012

I was actually refering to yoda code.

l j
Member #10,584
January 2009
avatar

To clarify.
if(jump = true && input.IsKeyPressed(ev, ALLEGRO_KEY_UP))
If you had written this as: if(true = jump && input.IsKeyPressed(ev, ALLEGRO_KEY_UP))
The code wouldn't have compiled because you can't assign to a literal constant.
In fact, comparing to true can be skipped entirely. the error here is an assignment where you most likely wanted to do a comparison.

PamperBoy
Member #15,477
January 2014

Nahaa sorry for not understandingXD I do see that jump = true has no use. But this is not the cause of the problem. the problem is i keep falling and whatever button i press nothing happens. except jump. then i go back to the spawnpoint. (same goes for hitting a platform)

Thomas Fjellstrom
Member #476
June 2000
avatar

it probably is the cause as you're assigning true to jump when you should be checking.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Werwolf696
Member #15,203
June 2013
avatar

Yes, they are saying that your code:

if(jump = true && input.IsKeyPressed(ev, ALLEGRO_KEY_UP))

Should actually read:

if(jump == true && input.IsKeyPressed(ev, ALLEGRO_KEY_UP))

'==' is for comparisons, '=' is for assignment.

PamperBoy
Member #15,477
January 2014

aah in the tut it was like =. ok, now when i press up it will only jump once wich is good. But i still keep falling till i hit a platform and then i go back to the spawnpoint. :/

edit

If you need some explanation, or want to see my header files just ask.

also the SetPosition function is needed to let me fall and jump. but i think it's also making the player fall continuously. maybe something is wrong in there:/ But yet again i did exactly the same as in the tutorial.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

PamperBoy said:

if(jump = true && input.IsKeyPressed(ev, ALLEGRO_KEY_UP)) {
vely = jumpspeed; platform = false; jump = false; //vDir = 1; }

You're assigning jump to true there, not checking for equivalence. That is why you can jump at any time. You also know this already so never mind. I wrote this post a couple hours ago.

Quote:

#SelectExpand
1void Player::SetPosition() {
2 if (vely >- 0) {
3 vDir = 2; 4 } 5 if(platform == false) { 6 vely += gravity; 7 } 8 else { 9 vely = 0; 10 }
11 x =+ velx;
12 y =+ vely;
13 x2 = x + width; 14 y2 = y + height; 15}

You're checking if vely is greater than negative zero, not if it is greater than or equal to zero. Also, you're assigning x the value of positive velx and y the value of positive vely. That is why your position resets. You should be using += there, not =+.

I did not check your collision code though. I'll look later.

PamperBoy
Member #15,477
January 2014

OMG thanks a bunch! it works!!! tho i have 1 question. in my inputmanager i use key presses for a single key press (i think thats correct) im wondering how i can make it so that when i press it i moves and when i let go it stops. (not that i need it in this project but i want to know^^)

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Go to: