我正试图让我在 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 文件。