Search code examples
lualove2d

love2d player jumping downwards


I'm trying to get my player in Love2d to jump. He jumps, yes, but downwards. Yeah, downwards. Like into the ground. I need some help figuring this out, I've played around with the data as much as I can and so far every logical solution (setting a jump height to a negative number, etc) doesn't work.

Here's the code, hope you guys can help.

-----------------
--- LOVE.LOAD ---
-----------------
function love.load()
    love.graphics.setBackgroundColor(92,217,255)
    playerIdle=love.graphics.newImage('/sprites/spriteTestIdle.png')
    playerLeft=love.graphics.newImage('/sprites/spriteTestFlip.png')
    playerRight=love.graphics.newImage('/sprites/spriteTest.png')
    player={}
    player.image=playerIdle
    player.x=400
    player.y=303
    player.speed=200
    player.y_velocity=303
    gravity=600
    jumpHeight=200
    hills=love.graphics.newImage('/sprites/spriteHills.png')
end
-------------------
--- LOVE.UPDATE ---
-------------------
function love.update(dt)

    if (player.x>735) then

        if (love.keyboard.isDown('left') or love.keyboard.isDown('a') or love.keyboard.isDown('right') or love.keyboard.isDown('d')) then
            player.x=player.x-(player.speed*dt)
        end

    elseif (player.x<-10) then

        if (love.keyboard.isDown('left') or love.keyboard.isDown('a') or love.keyboard.isDown('right') or love.keyboard.isDown('d')) then
            player.x=player.x+(player.speed*dt)
        end

    else

        if (love.keyboard.isDown('right') or love.keyboard.isDown('d')) then
            player.image=playerRight
            player.x=player.x+(player.speed*dt)
        elseif (love.keyboard.isDown('left') or love.keyboard.isDown('a')) then
            player.image=playerLeft
            player.x=player.x-(player.speed*dt)
        else
            player.image=playerIdle
            player.x=player.x
        end

    end

    if (player.y_velocity ~= 303) then
        player.y = player.y + player.y_velocity * dt
        player.y_velocity = player.y_velocity - gravity * dt

        if (player.y < 303) then
            player.y_velocity = 303
            player.y = 303
        end

    end


end
-----------------------
--- LOVE.KEYPRESSED ---
-----------------------
function love.keypressed(key)

    if (key == " ") then

        if (player.y_velocity == 303) then
            player.y_velocity = jumpHeight
        end

    end

end
-----------------
--- LOVE.DRAW ---
-----------------
function love.draw()
    love.graphics.draw(hills, 0, 0)
    love.graphics.draw(player.image, player.x, player.y)
end

Here is the main.love file.


Solution

  • I think your problem is that you don't see the coordinate system in the good way.

    enter image description here

    While you go down the Y value is increasing! Not decreasing like you see in most math classes..

    To fix your main.lua you should just replace those:

    jumpHeight= -200 --in love.load
    
    player.y_velocity = player.y_velocity - gravity * dt --in the love.update
    
    if (player.y > 303) then --in love.update
    

    I hope this is clear for you, good luck for your game (I like the graphics).

    Also, if you want help faster you should directly post to Love2d Forums.