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.
Jumping not working well
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- ixjackinthebox
- Prole
- Posts: 45
- Joined: Sun Apr 29, 2012 6:00 pm
Jumping not working well
- Attachments
-
- Game1.love
- (60.97 KiB) Downloaded 178 times
-
- Prole
- Posts: 44
- Joined: Sat Dec 15, 2012 7:55 am
Re: Jumping not working well
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:
I hope this helps!
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
- ixjackinthebox
- Prole
- Posts: 45
- Joined: Sun Apr 29, 2012 6:00 pm
Re: Jumping not working well
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.
Also when I press "a" and "d" it sometimes jumps.
- Attachments
-
- Game1.love
- (61.19 KiB) Downloaded 156 times
- ixjackinthebox
- Prole
- Posts: 45
- Joined: Sun Apr 29, 2012 6:00 pm
Re: Jumping not working well
OK I think I did it wrong before I was doing
When I should do
But is there any other way to do a jump better?
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
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
Re: Jumping not working well
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.
-
- Prole
- Posts: 44
- Joined: Sat Dec 15, 2012 7:55 am
Re: Jumping not working well
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:
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:
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:
I hope this helps! Have fun coding!
Code: Select all
-- line 12
x_velocity, y_velocity = Objects.Player.body:getLinearVelocity( )
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
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
- ixjackinthebox
- Prole
- Posts: 45
- Joined: Sun Apr 29, 2012 6:00 pm
Re: Jumping not working well
Thank you so much rexjericho
Who is online
Users browsing this forum: Bing [Bot] and 3 guests