love2d玩家向下跳

时间:2013-02-13 18:26:49

标签: lua love2d

我正试图让我的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

Here is main.love文件。

1 个答案:

答案 0 :(得分:4)

我认为你的问题是你没有很好地看到坐标系。

enter image description here

当你下降时,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 Forums

相关问题