Problem with 2D side scrolling jump function

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
alasqa
Prole
Posts: 3
Joined: Mon Apr 22, 2013 12:48 am

Problem with 2D side scrolling jump function

Post by alasqa »

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:

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
This section of code is in the keypressed function:

Code: Select all

if key == " " then
		if player.y_velocity == 0 then
			player.y_velocity = jump_height
		end
	end
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.
alasqa
Prole
Posts: 3
Joined: Mon Apr 22, 2013 12:48 am

Re: Problem with 2D side scrolling jump function

Post by alasqa »

Okay well I fixed the problem, turns out I had missed one vital part of the code.

I had to make sure to put a negative next to the player.y variable

Code: Select all

loveg.draw(player.image, player.x, - player.y, 0, 1, 1)
However, this causes another issue. I have my player.y set to 535, so when it goes negative, it goes off the screen. This is the only fix I could find for the upwards jumping. Any ideas on how to fix this next issue?
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Problem with 2D side scrolling jump function

Post by micha »

Better use positive coordinates:

Code: Select all

loveg.draw(player.image, player.x, - player.y, 0, 1, 1)
and reverse the sign of gravity and jump hight.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests