Jumping problem...
Posted: Sun Mar 17, 2013 8:38 pm
Hello guys, my first time here.
I'm trying to learn how to make a platform game and i need to make the player jump. The main problem is, by the code i have, the player only jumps when the spacebar is pressed, if i release it, the player stops in the air and, when i press it again the plays continue to move down. The code:
Main.lua
Player.lua
OBS: The -- Comments are in portuguese language, ignore it.
EDIT: Hey guys! Managed to fix it. Just change the position of some stuff and worked.
Here's the changes:
I'm trying to learn how to make a platform game and i need to make the player jump. The main problem is, by the code i have, the player only jumps when the spacebar is pressed, if i release it, the player stops in the air and, when i press it again the plays continue to move down. The code:
Main.lua
Code: Select all
require "player"
function love.load()
love.graphics.setBackgroundColor(35, 35, 35) -- Cor de Fundo
player.load() -- Lê o jogador
end
function love.update(dt)
player.move(dt) -- Movimenta o jogador
player.boundary(dt) -- Verifica se o jogador não passa das paredes
player.jump(dt) -- Faz o jogador pular
end
function love.draw()
love.graphics.setColor(120, 120, 120) -- Cor do Chão
love.graphics.rectangle("fill", 0, 350, 400, 50) -- Chão
player.draw() -- Desenha o jogador
end
Code: Select all
player = {}
function player.load()
player.width = 10
player.height = 10
player.x = 5
player.y = 340
player.mass = 60
player.speed = 175
player.y_speed = 0
end
function player.draw()
love.graphics.setColor(255, 0, 0) -- vermelho
love.graphics.rectangle("fill", player.x, player.y, player.width, player.height) -- retangulo
end
function player.move(dt)
if (love.keyboard.isDown("a")) then -- Andar para a esquerda
player.x = player.x - player.speed * dt
elseif (love.keyboard.isDown("d")) then -- Andar para a direita
player.x = player.x + player.speed * dt
elseif (love.keyboard.isDown("w")) then -- Andar para cima (Ainda não é pular)
player.y = player.y - (player.speed) * dt
elseif (love.keyboard.isDown("s")) then -- Andar para baixo
player.y = player.y + (player.speed) * dt
end
end
function player.jump(dt) -- POR FAZER
if player.y_speed == 0 then
player.y_speed = 220
end
if (love.keyboard.isDown(" ")) then
player.y = player.y - player.y_speed*dt
player.y_speed = player.y_speed - 9.8 * player.mass * dt
end
if (player.y == 350 - player.height) then
player.y_speed = 0
end
end
function player.boundary(dt)
if (player.x < 0) then -- não passa do lado esquerdo
player.x = 0
elseif (player.x > 400 - player.width) then -- não passa do lado direito
player.x = 400 - player.width
elseif (player.y > 350 - player.height) then -- não passa do chão
player.y = 350 - player.height
elseif (player.y < 0) then -- não passa do teto
player.y = 0
end
end
EDIT: Hey guys! Managed to fix it. Just change the position of some stuff and worked.
Here's the changes:
Code: Select all
function player.jump(dt) -- POR FAZER
if (love.keyboard.isDown(" ") and player.y_speed == 0) then
player.y_speed = 220
end
player.y = player.y - player.y_speed*dt
player.y_speed = player.y_speed - 9.8 * player.mass * dt
if (player.y > 350 - player.height) then
player.y = 350 - player.height
player.y_speed = 0
end
end