Jumping not working well

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
ixjackinthebox
Prole
Posts: 45
Joined: Sun Apr 29, 2012 6:00 pm

Jumping not working well

Post by ixjackinthebox »

Well when I press space bar sometimes the player does not jump but sometimes he does
and is there a better way to make the player jump.
Attachments
Game1.love
(60.97 KiB) Downloaded 182 times
rexjericho
Prole
Posts: 44
Joined: Sat Dec 15, 2012 7:55 am

Re: Jumping not working well

Post by rexjericho »

The problem is in the type of function your are using to handle your player's jumping, which is inside your love.update function. Love2d creates the frames of your game by alternating between the functions love.update() and love.draw() over and over again until forever. The function in GameCode.lua at line 28, love.keyboard.isDown(" "), returns true if the space key is down and false if it is not. Since this function is in love.update(), if love happens to be doing the love.draw() function at the time, the love.keyboard.isDown() function wont be executed and your player wont jump. The function will only happen when love is in the love.update() part of your code. For example, when love makes your frame, if it takes 50% of the time to update your game, and 50% of the time to draw your game, the player will only jump about 50% of the time that you hit the spacebar.

There is another function you can use called love.keypressed(). Here's a link to the documentation: https://love2d.org/wiki/love.keypressed

The great thing about this function is that it is executed whenever a button is pressed, and doesn't matter if love is doing the update or draw function. You can use it like this:

Code: Select all

-- put this wherever you'd put a regular function
function love.keypressed(key)
    -- if the key that was just pressed is the space key:
    if key == " " then
        -- do all your jumping stuff
    end
end
I hope this helps!
User avatar
ixjackinthebox
Prole
Posts: 45
Joined: Sun Apr 29, 2012 6:00 pm

Re: Jumping not working well

Post by ixjackinthebox »

I did what you said and it still doesn't work well can you have a look at it and tell me if I have done it wrong.
Also when I press "a" and "d" it sometimes jumps.
Attachments
Game1.love
(61.19 KiB) Downloaded 160 times
User avatar
ixjackinthebox
Prole
Posts: 45
Joined: Sun Apr 29, 2012 6:00 pm

Re: Jumping not working well

Post by ixjackinthebox »

OK I think I did it wrong before I was doing

Code: Select all

-- put this wherever you'd put a regular function
function love.keypressed(key)
    -- if the key that was just pressed is the space key:
    if key == " " or "w" or "up" then
        -- do all your jumping stuff
    end
end
When I should do

Code: Select all

-- put this wherever you'd put a regular function
function love.keypressed(key)
    -- if the key that was just pressed is the space key:
    if key == " " then
        -- do all your jumping stuff
    end
end
But is there any other way to do a jump better?
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: Jumping not working well

Post by Lafolie »

Logic OR doesn't work like it does in regular speech. If you want to check for several keys in the same expression you'd do:

Code: Select all

if key == " " or key == "w" or key == "up" then
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
rexjericho
Prole
Posts: 44
Joined: Sat Dec 15, 2012 7:55 am

Re: Jumping not working well

Post by rexjericho »

Hey, I'm not familiar with how the physics module works, but I think I've located the problem in line 12 of GameCode.lua:

Code: Select all

-- line 12
x_velocity, y_velocity = Objects.Player.body:getLinearVelocity( )
It seems that after your guy lands from the jump, the y_velocity does not always equal exactly 0. The y_velocity is actually a very small number, but not 0. This may be caused by rounding errors in the physics module. This is causing your keypressed function not to work as expected:

Code: Select all

function love.keypressed(key)
	--jump
	if key == " " or key == "up" or key == "w" and y_velocity == 0 then
		Objects.Player.body:setLinearVelocity( x_velocity, Players_jump_height )
		if y_velocity ~= 0 then
			Objects.Player.body:setLinearVelocity( x_velocity, 0 )
		end
	end
end
According to your if statement, in order for your player to jump, y_velocity must equal exactly 0. One way that this can be fixed is by checking the y_velocity value, and if that value is close to 0, then to just go ahead and set y_velocity = 0. This could be done right after line 12 like so:

Code: Select all

-- line 12
x_velocity, y_velocity = Objects.Player.body:getLinearVelocity( )

-- This checks if y_velocity is very close to 0. 
-- If y_velocity is between -0.001 and 0.001, then y_velocity gets set to 0
if y_velocity > -0.001 and y_velocity < 0.001 then
	y_velocity = 0
end
I hope this helps! Have fun coding!
User avatar
ixjackinthebox
Prole
Posts: 45
Joined: Sun Apr 29, 2012 6:00 pm

Re: Jumping not working well

Post by ixjackinthebox »

Thank you so much rexjericho
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 12 guests