Jumping help
Posted: Sat Feb 01, 2014 10:22 pm
So I've been following sockmunkee dev's tutorial on youtube on how to make a platformer http://www.youtube.com/watch?v=Ueya18MrtaA and it's a good tutorial, but he hasnt uploaded any more videos after the second in the platforming series, and i feel like watching his older tutorials wont work with the code that he shows in these platforming videos. I have figured out how to get gravity and jumping to work, but the jumping looks stiff and you can jump multiple times, and if you hit the "ceiling" of the game window, you can't come back down. Can anyone help me?
My player.lua file:
and my main.lua file:
My player.lua file:
Code: Select all
player = {}
function player.load()
player.x = 5
player.y = 5
player.xvel = 0
player.yvel = 200
player.friction = 7
player.speed = 2250
player.width = 50
player.height = 50
player.jumpHeight = 200
player.startJump = 0
player.jumping = false
player.screenheight = 900
end
function player.draw()
love.graphics.setColor(0,255,0)
love.graphics.rectangle("fill",player.x,player.y,player.width,player.height)
end
function player.physics(dt)
player.x = player.x + player.xvel * dt
player.y = player.y + player.yvel * dt
if not player.jumping then
player.yvel = player.yvel + gravity * dt
else
if player.startJump - player.y >= player.jumpHeight then
player.jumping = false
player.yvel = 200
end
end
player.xvel = player.xvel * (1 - math.min(dt*player.friction, 1))
end
function player.move(dt)
if love.keyboard.isDown("right") and
player.xvel < player.speed then
player.xvel = player.xvel + player.speed * dt
end
if love.keyboard.isDown("left") and
player.xvel > -player.speed then
player.xvel = player.xvel - player.speed * dt
end
if love.keyboard.isDown("up") and not player.jumping then
player.yvel = player.yvel - player.speed * dt
player.startJump = player.y
player.jumping = true
if player.y + player.height + player.width > player.screenheight then
gravity = 600
player.y = player.screenheight
end
end
end
function player.boundary()
if player.x < 0 then
player.x = 0
player.xvel = 0
end
if player.y + player.height >= groundLevel then
player.y = groundLevel - player.height
player.yvel = 0
end
end
function UPDATE_PLAYER(dt)
player.move(dt)
player.physics(dt)
player.boundary()
end
function DRAW_PLAYER()
player.draw()
end
Code: Select all
require "player"
function love.load()
love.graphics.getBackgroundColor(0,0,0)
groundLevel = 600
gravity = 900
--Loading classes
player.load()
end
function love.update(dt)
UPDATE_PLAYER(dt)
if player.yvel ~= 0 then
player.y = player.y + player.yvel * dt
player.yvel = player.yvel - gravity * dt
if player.y < 0 then
player.yvel = 0
player.y = 0
end
end
end
function love.draw()
DRAW_PLAYER()
end