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
Player
Collision
InputManager
Map
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.
Well thx for referring me to yoda. But im pretty sure that is not causing the problem.
I was actually refering to yoda code.
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.
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)
it probably is the cause as you're assigning true to jump when you should be checking.
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.
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.
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.
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.
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^^)
When you get a left/right key press, set velx to non-zero. When you get a corresponding key up, set velx to zero.