Page 1 of 1
Jumping not working well
Posted: Fri Dec 14, 2012 8:34 pm
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.
Re: Jumping not working well
Posted: Sat Dec 15, 2012 9:37 am
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!
Re: Jumping not working well
Posted: Sat Dec 15, 2012 4:19 pm
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.
Re: Jumping not working well
Posted: Sun Dec 16, 2012 9:12 pm
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?
Re: Jumping not working well
Posted: Mon Dec 17, 2012 4:02 pm
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
Re: Jumping not working well
Posted: Mon Dec 17, 2012 9:57 pm
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!
Re: Jumping not working well
Posted: Tue Dec 18, 2012 2:09 pm
by ixjackinthebox
Thank you so much rexjericho