Problem with 2D side scrolling jump function
Posted: Wed Sep 18, 2013 9:10 pm
Hey yo I'm working on a space adventure/2D side scroller and I am now having issues with my jump code. I can get the character to jump with the following code, but instead of jumping upwards, my character jumps downward. I have messed around with it for a while changing the variables to negative and what not. Still to no avail. I followed a tutorial online and got the following code.
This section is in the update function:
This section of code is in the keypressed function:
Now before I took the code straight from the tutorial I had it written out on my own. That didn't work so I went ahead and just tried to copy and paste it and just adjust a few numbers. That didn't work either. Anyone have any ideas whats going on or have a better way to implement jumping into a game.
This section is in the update function:
Code: Select all
if player.y_velocity ~= 0 then
player.y = player.y + player.y_velocity * dt
player.y_velocity = player.y_velocity - gravity * dt
if player.y < 535 then
player.y_velocity = 0
player.y = 535
end
end
Code: Select all
if key == " " then
if player.y_velocity == 0 then
player.y_velocity = jump_height
end
end