When the player bumps into a wall, the character freezes and can no longer move.
My code for collision:
Code: Select all
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
return x1 < x2+w2 and
x2 < x1+w1 and
y1 < y2+h2 and
y2 < y1+h1
end
function cols()
for i, wall in ipairs(walls) do
if(CheckCollision(player.x, player.y, player.sprite:getWidth(), player.sprite:getHeight(), wall.x, wall.y, wall.w, wall.h)) then
if(wall.collide) then
return true
else
return false
end
end
end
end
Code: Select all
function love.update(dt)
if(love.keyboard.isDown('escape')) then
love.event.push('quit')
end
if(love.keyboard.isDown('up','w')) then
if(not cols()) then
player.y = player.y - (player.speed * dt)
end
end
if(love.keyboard.isDown('down','s')) then
if(not cols()) then
player.y = player.y + (player.speed * dt)
end
end
if(love.keyboard.isDown('left','a')) then
if(not cols()) then
player.x = player.x - (player.speed * dt)
end
end
if(love.keyboard.isDown('right','d')) then
if(not cols()) then
player.x = player.x + (player.speed * dt)
end
end
end
Code: Select all
player = { x = 100, y = 100, speed = 150, sprite = love.graphics.newImage('char.png') }
walls = {
{ fillMode = "fill", x = 50, y = 50, w = 100, h = 20, collide = true },
{ fillMode = "line", x = 50 + 100, y = 50, w = 100, h = 20, collide = false }
}