love2d - 如何让实体玩家在按下Up或W时跳跃

时间:2013-07-16 21:15:19

标签: lua love2d

我如何让实体播放器在按下Up或W时跳转,我搜索和搜索但是没有看到任何方法可以使用我的代码。目前,当按下向上或向上时,角色只是上升而其保持没有边界并且不会下降。这就是我到目前为止所得到的:

    function love.load() -- On game load
    love.mouse.setVisible(false) -- Makes cursor invisible
    imageCloud = love.graphics.newImage("textures/cloud.png") -- Loading cloud image
    imageCloud2 = love.graphics.newImage("textures/cloud.png") -- Loading cloud image
    imageCloud3 = love.graphics.newImage("textures/cloud.png") -- Loading cloud image
    imageBg = love.graphics.newImage("textures/background.png") -- Loading background image
    imageMountains = love.graphics.newImage("textures/mountains.png") -- Loading mountains image
    imageSky = love.graphics.newImage("textures/sky.jpg") -- Loading mountains image
    imageGround = love.graphics.newImage("textures/ground.png") -- Loading ground image
    playerleft = love.graphics.newImage("textures/playerleft.png") -- Loading player left
    playerright = love.graphics.newImage("textures/playerright.png") -- Loading player right
    playershoot = love.graphics.newImage("textures/playershoot.png") -- Loading player shoot
    love.graphics.setBackgroundColor( 0, 128, 255 ) -- Color for background
    print("Zonisto started succesfully") -- Console text for game starting up
    player = playerright -- Sets player to start in right position 
    xCloud1 = 0 -- Sets Cloud1's starting position
    xCloud2 = 45 -- Sets Cloud2's starting position
    xCloud3 = 160 -- Sets Cloud3's starting position
    xPlayer = 0 -- Sets Player's starting position
    playery = 390 -- Sets Player's starting height
    shootsound = love.audio.newSource("sounds/shoot.wav", "static") -- Loads shooting sound
    jumpsound = love.audio.newSource("sounds/jump.mp3", "static") -- Loads shooting sound
    theme = love.audio.newSource("sounds/theme.mp3") -- Loads theme music
    love.audio.play(theme) --Plays theme music 
    theme:setVolume(0.1) -- Sets theme music to 10%
end

function love.draw() -- On frame refresh

    local x = love.mouse.getX( ) -- Getting x position for cursor follower
    local y = love.mouse.getY( ) -- Getting y position for cursor follower

    --Sky start
    love.graphics.setColor( 255, 255, 255, 255 )    -- Sets transparency for Mountains
    love.graphics.draw( imageSky, 0, 0, 0, 1, 1, 0, 0) -- Mountains image
    --Sky end

    --Mountains start
    love.graphics.setColor( 255, 255, 255, 255 )    -- Sets transparency for Mountains
    love.graphics.draw( imageMountains, 0, 300, 0, 1, 1, 0, 0) -- Mountains image
    --Mountains end

    --Version area start
    love.graphics.setColor( 255, 255, 255, 100 )    -- Color for Version box
    love.graphics.rectangle( "fill", 0, 0, 165, 15 ) -- Shape for Version box

    love.graphics.setColor( 0, 128, 255, 255 ) -- Color for Version text
    love.graphics.print( "Zonisto: Pre-Alpha V0.001", 1, 1, 0, 1, 1 ) -- Version text
    --Version area end

    --Ground start
    love.graphics.setColor( 255, 255, 255, 255 )    -- Sets transparency for Ground
    love.graphics.draw( imageGround, 0, 500, 0, 1, 1, 0, 0) -- Ground image
    --Ground end

    --Player start
    love.graphics.setColor( 255, 255, 255, 255 )    -- Sets transparency for Player
    love.graphics.draw( player, xPlayer, playery, 0, 1, 1, 0, 0) -- Player image
    --Player end

    --Cloud1 start
    love.graphics.setColor( 255, 255, 255, 255 )    -- Sets transparency for Cloud1
    love.graphics.draw( imageCloud, xCloud1 - 256, 40, 0, 1, 1, 0, 0) -- Cloud1 image
    if xCloud1 >= 1050 then -- Detects when cloud1 surpasses boundaries
        xCloud1 = 0 -- Sets position for cloud1 to return to
        end
    --Cloud1 end

    --Cloud2 start
    love.graphics.setColor( 255, 255, 255, 95 ) -- Sets transparency for Cloud2
    love.graphics.draw( imageCloud2, xCloud2 - 456, 90, 0, 1, 1, 0, 0) -- Cloud2 image
    if xCloud2 >= 1050 then -- Detects when cloud2 surpasses boundaries
        xCloud2 = 0 -- Sets position for cloud2 to return to
        end
    --Cloud2 end

    --Cloud3 start
    love.graphics.setColor( 255, 255, 255, 155 )    -- Sets transparency for Cloud1
    love.graphics.draw( imageCloud3, xCloud3 - 356, 40, 0, 1, 1, 0, 0) -- Cloud1 image
    if xCloud3 >= 1050 then -- Detects when cloud3 surpasses boundaries
        xCloud3 = 0 -- Sets position for cloud3 to return to
        end
    --Cloud3 end

    --Cursor start
    love.graphics.setColor( 255, 255, 255, 205 )    -- Color for cursor follower
    love.graphics.circle( "fill", x, y, 8, 8) -- Shape for cursor follower  
    --Cursor end    

end

function love.update(dt) -- On frame update
    xCloud1 = xCloud1 + 32*dt -- Cloud movement
    xCloud2 = xCloud2 + 15*dt -- Cloud movement
    xCloud3 = xCloud3 + 48*dt -- Cloud movement

     if     love.keyboard.isDown("a") then -- Detects if a is being pressed
        player = playerleft -- Changes image to left view
     elseif love.keyboard.isDown("d") then -- Detects if d is being pressed
        player = playerright -- Changes image to right view
     end

     if     love.keyboard.isDown("left") then -- Detects if left is being pressed
        player = playerleft -- Changes image to left view
     elseif love.keyboard.isDown("right") then -- Detects if right is being pressed
        player = playerright -- Changes image to right view
     elseif love.keyboard.isDown("kpenter") then -- Detects if key pad enter is being pressed
        player = playershoot  -- Changes to shooting view
        love.audio.play(shootsound) -- Plays shooting sound
     elseif love.keyboard.isDown("enter") then -- Detects if enter is being pressed
        player = playershoot -- Changes to shooting view
        love.audio.play(shootsound) -- Plays shooting sound
     elseif love.keyboard.isDown("return") then -- Detects if return is being pressed
        player = playershoot -- Changes to shooting view
        love.audio.play(shootsound) -- Plays shooting sound
     end

     if     love.keyboard.isDown("up") then -- Detects if up is being pressed
        playery = playery - 300 * dt
        love.audio.play(jumpsound) -- Plays jumping sound
     elseif love.keyboard.isDown("w") then -- Detects if w is being pressed
        love.audio.play(jumpsound) -- Plays jumping sound
     end


    if love.keyboard.isDown("right") then -- Detects if right is pressed
        print("Moving right") -- Prints in console that player is being moved right
        xPlayer = xPlayer + 48*dt -- Sets timing for amount the player moves
    end 

    if love.keyboard.isDown("left") then -- Detects if left is pressed
        print("Moving left") -- Prints in console that player is being moved left
        xPlayer = xPlayer + -48*dt -- Sets timing for amount the player moves
    end 

    if love.keyboard.isDown("d") then -- Detects if d is being presssed
        print("Moving right") -- Prints in console that player is being moved right
        xPlayer = xPlayer + 48*dt -- Sets timing for amount the player moves
    end 

    if love.keyboard.isDown("a") then -- Detects if a is being pressed
        print("Moving left") -- Prints in console that player is being moved left
        xPlayer = xPlayer + -48*dt -- Sets timing for amount the player moves 
    end 

end

function love.focus(bool) -- On game focus/unfocus
    if bool then
        print("Game focused") -- Console text for game being focused on
    else
        print("Game not focused") -- Console text for game not being focused on
        end
end

function love.keypressed( key, unicode ) -- On key press
        print ("Input pressed: " .. key) -- Console text for key being pressed
end

function love.keyreleased( key, unicode ) -- On key release
        print ("Input released: " .. key) -- Console text for key being released
end

function love.mousepressed( x, y, button ) -- On mouse press
        print ("Mouse pressed: " .. x, y, button) -- Console text for mouse being pressed
end

function love.mousereleased( x, y, button ) -- On mouse release
        print ("Mouse released: " .. x, y, button) -- Console text for mouse being released
end

function love.quit() -- On game exit
        print ("Exiting Zonisto... goodbye!") -- Console text for game closing
end

1 个答案:

答案 0 :(得分:1)

你在这里说的是:

 if     love.keyboard.isDown("up") then 
    playery = playery - 300 * dt
    love.audio.play(jumpsound) -- Plays jumping sound
 elseif love.keyboard.isDown("w") then -- Detects if w is being pressed
    love.audio.play(jumpsound) -- Plays jumping sound
 end

如果 仅在 w或更高时按下,请向上移动播放器。你需要做的是创建一个函数,比如这个

function love.load()
    --rest of code
    playerCanJump = true
end

if  love.keyboard.isDown("up") and playerCanJump then -- Detects if up is being pressed
    jy = playery
    playerJump()
 elseif love.keyboard.isDown("w") and playerCanJump then -- Detects if w is being pressed
    jy = playery
    playerJump()
 end

function playerJump()
    local dt = love.timer.getDelta()
    playerCanJump = false
    playery = playery - 300 * dt
    love.audio.play(jumpsound)
end

我会留下这个作业:让它在跳跃一定高度后跌倒。 提示:它使用jy变量 对不起,如果代码看起来很混乱,我试着用你的风格编码,但我觉得我有点失败......