I am encountering a problem in my tile collision method. For some reason the player is able to pass through some tiles when it shouldn't be able to. Also, I'm not entirely sure why but when it gets stuck it can move left through objects, but only left. I have posted some code below, and it would be nice if somebody could point me in the right direction. (Or even better if somebody could find a quick solution!) My player movement method and the tile collision method are both called in the update method.
map = { {1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
{1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
{1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
{1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
{1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
{1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
{2,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
{2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
{2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
{2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
{2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
{2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
{2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
{2,2,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
{2,2,1,1,1,3,3,1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
{2,2,1,1,1,3,3,1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
{2,2,1,1,1,3,3,1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
{1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
{1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
{1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
}
function testTile(x,y)
if map[y][x + 1] == 1 then
canRight = true
end
if map[y][x + 1] ~= 1 then
canRight = false
end
if map[y][x - 1] == 1 then
canLeft = true
end
if map[y][x - 1] ~= 1 then
canRight = false
end
if map[y + 1][x] == 1 then
canDown = true
end
if map[y + 1][x] ~= 1 then
canDown = false
end
if map[y - 1][x] == 1 then
canUp = true
end
if map[y - 1][x] ~= 1 then
canUp = false
end
end
function movePlayer(dt)
if love.keyboard.isDown("right") and canRight then
playerX = playerX + 1 * dt
end
if love.keyboard.isDown("left") and canLeft then
playerX = playerX - 1 * dt
end
if love.keyboard.isDown("down") and canDown then
playerY = playerY + 1 * dt
end
if love.keyboard.isDown("up") and canUp then
playerY = playerY - 1 * dt
end
end
The reason left is working when it's stuck is likely because you have a typo in the 4th if
statement of testTile(x,y)
.
You wrote
if map[y][x - 1] ~= 1 then
canRight = false
end
and it should be
if map[y][x - 1] ~= 1 then
canLeft = false
end