I cant figure out how to make my character jump and only be able to jump once
Posted: Fri Jul 05, 2024 6:50 pm
When I press W, I just slowly move up and don't fall until I stop pressing W. I tried to make collision detection to make its so you can only jump when touching the ground, but I just make it so I couldn't jump as it would stop me from moving in the air.
Here is the kinda relevant code (british is one of the players):
world = wf.newWorld(0,2000)
-- Declare open source code above this line /\
love.graphics.setDefaultFilter('nearest', 'nearest') -- Makes the images look crisper and not blurry
cam = camera()
british = {}
british.x = 400
british.y = 200
british.speed = 140
british.collider = world:newBSGRectangleCollider(british.x, british.y, 50, 39, 5)
british.collider:setFixedRotation(true)
map = love.graphics.newImage("Maps/Plains map.png")
british.spriteSheet = love.graphics.newImage("sprites/British musketeer1-sheet-sheet.png")
british.grid = anim8.newGrid(50, 40, british.spriteSheet:getWidth(), british.spriteSheet:getHeight())
british.animations = {}
british.animations.walkRight = anim8.newAnimation(british.grid('1-10', 1), 0.15)
british.animations.walkLeft = anim8.newAnimation(british.grid('50-40', 1), 0.15)
british.animations.jumpRight = anim8.newAnimation(british.grid('12-16', 1), 0.15)
british.animations.jumpLeft = anim8.newAnimation(british.grid('36-40', 1), 0.15)
british.animations.idleRight = anim8.newAnimation(british.grid('1-1', 1), 0.15)
british.animations.idleLeft = anim8.newAnimation(british.grid('50-50', 1), 0.15)
british.animations.fireRight = anim8.newAnimation(british.grid('17-18', 1), 0.15)
british.anim = british.animations.walkRight
local ground = world:newRectangleCollider(0, 480, 800, 120)
ground:setType('static')
ground.y = 120
end
function love.update(dt)
local ismoving = false
local vx = 0 -- velocity of x
local vy = 0 -- velocity of y
if love.keyboard.isDown("d") then
vx = british.speed
british.anim = british.animations.walkRight
ismoving = true
end
if love.keyboard.isDown("a") then
vx = british.speed * -1 --Turns the velocity negitive to move left
british.anim = british.animations.walkLeft
ismoving = true
end
if love.keyboard.isDown("s") then
vy = british.speed
end
if love.keyboard.isDown("w") then
vy = british.speed * -1 --Turns the velocity negitive to jump up
british.anim = british.animations.jumpRight
end
if love.keyboard.isDown("space") then
british.anim = british.animations.fireRight
end
british.collider:setLinearVelocity(vx, vy)
if ismoving == false then
british.anim:gotoFrame(1)
end
if british.y > 220 then
vy = british.speed + 2000
end
british.anim:update(dt)
world:update(dt)
british.x = british.collider:getX()
british.y = british.collider:getY()
cam:lookAt(british.x, british.y)
local w = love.graphics.getWidth()
local h = love.graphics.getHeight()
if cam.x < w/2 then
cam.x = w/2
end
if cam.y < h/2 then
cam.y = h/2
end
local mapW = 800
local mapH = 600
if cam.x > (mapW - w/2) then
cam.x = (mapW - w/2)
end
if cam.y > (mapH - h/2) then
cam.y = (mapH - h/2)
end
end
function love.draw()
cam:attach()
love.graphics.draw(map, 0, 0)
british.anim:draw(british.spriteSheet, british.x, british.y, nil, nil, nil, 16.5, 19.5)
world:draw()
cam:detach()
end
Here is the kinda relevant code (british is one of the players):
world = wf.newWorld(0,2000)
-- Declare open source code above this line /\
love.graphics.setDefaultFilter('nearest', 'nearest') -- Makes the images look crisper and not blurry
cam = camera()
british = {}
british.x = 400
british.y = 200
british.speed = 140
british.collider = world:newBSGRectangleCollider(british.x, british.y, 50, 39, 5)
british.collider:setFixedRotation(true)
map = love.graphics.newImage("Maps/Plains map.png")
british.spriteSheet = love.graphics.newImage("sprites/British musketeer1-sheet-sheet.png")
british.grid = anim8.newGrid(50, 40, british.spriteSheet:getWidth(), british.spriteSheet:getHeight())
british.animations = {}
british.animations.walkRight = anim8.newAnimation(british.grid('1-10', 1), 0.15)
british.animations.walkLeft = anim8.newAnimation(british.grid('50-40', 1), 0.15)
british.animations.jumpRight = anim8.newAnimation(british.grid('12-16', 1), 0.15)
british.animations.jumpLeft = anim8.newAnimation(british.grid('36-40', 1), 0.15)
british.animations.idleRight = anim8.newAnimation(british.grid('1-1', 1), 0.15)
british.animations.idleLeft = anim8.newAnimation(british.grid('50-50', 1), 0.15)
british.animations.fireRight = anim8.newAnimation(british.grid('17-18', 1), 0.15)
british.anim = british.animations.walkRight
local ground = world:newRectangleCollider(0, 480, 800, 120)
ground:setType('static')
ground.y = 120
end
function love.update(dt)
local ismoving = false
local vx = 0 -- velocity of x
local vy = 0 -- velocity of y
if love.keyboard.isDown("d") then
vx = british.speed
british.anim = british.animations.walkRight
ismoving = true
end
if love.keyboard.isDown("a") then
vx = british.speed * -1 --Turns the velocity negitive to move left
british.anim = british.animations.walkLeft
ismoving = true
end
if love.keyboard.isDown("s") then
vy = british.speed
end
if love.keyboard.isDown("w") then
vy = british.speed * -1 --Turns the velocity negitive to jump up
british.anim = british.animations.jumpRight
end
if love.keyboard.isDown("space") then
british.anim = british.animations.fireRight
end
british.collider:setLinearVelocity(vx, vy)
if ismoving == false then
british.anim:gotoFrame(1)
end
if british.y > 220 then
vy = british.speed + 2000
end
british.anim:update(dt)
world:update(dt)
british.x = british.collider:getX()
british.y = british.collider:getY()
cam:lookAt(british.x, british.y)
local w = love.graphics.getWidth()
local h = love.graphics.getHeight()
if cam.x < w/2 then
cam.x = w/2
end
if cam.y < h/2 then
cam.y = h/2
end
local mapW = 800
local mapH = 600
if cam.x > (mapW - w/2) then
cam.x = (mapW - w/2)
end
if cam.y > (mapH - h/2) then
cam.y = (mapH - h/2)
end
end
function love.draw()
cam:attach()
love.graphics.draw(map, 0, 0)
british.anim:draw(british.spriteSheet, british.x, british.y, nil, nil, nil, 16.5, 19.5)
world:draw()
cam:detach()
end