2013-07-16 2 views
0

Как я могу заставить субъект-плеер прыгать по нажатию Up или W, искать и искать, но никакой метод, который видел, не будет работать с моим кодом , В настоящий момент, когда нажата кнопка «Вверх» или «W», персонаж просто поднимается, а его удерживается без границ и не отбрасывается. Это то, что ив получил до сих пор: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

То, что вы здесь говорит, что это:

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 

Если и только если ш или до нажат, переместите игрок вверх. Что вам нужно сделать, это сделать функцию, как этот

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 Извините, если код выглядит сбивающим с толку, я попытался закодировать его в вашем стиле, но я думаю, что я как-то не сработал ...

+0

Спасибо ... не работает. –

+0

Я попробовал и получил: main.lua: 159: попытка выполнить арифметику по глобальному 'dt' (значение nil) –

+0

Это происходит только при нажатии кнопки up или w –

Смежные вопросы