0

我正试图让我在 Love2d 中的玩家跳跃。他跳,是的,但向下。是的,向下。如同入土。我需要一些帮助来解决这个问题,我已经尽可能多地使用数据,到目前为止,每个逻辑解决方案(将跳跃高度设置为负数等)都不起作用。

下面是代码,希望对大家有所帮助。

-----------------
--- 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

这是main.love 文件。

4

1 回答 1

4

我认为您的问题是您看不到坐标系的好方法。

在此处输入图像描述

当您下降时,Y 值正在增加!不像你在大多数数学课上看到的那样减少..

要修复你的 main.lua,你应该替换那些:

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

我希望这对你来说很清楚,祝你的游戏好运(我喜欢图形)。

此外,如果您需要更快的帮助,您应该直接发布到Love2d 论坛

于 2013-02-13T21:13:41.100 回答