Page 1 of 1

Tile collision problem

Posted: Fri Jul 26, 2013 2:16 am
by stanmarshd
I've been making a tile system so that sometimes collision is on, and other times is off.

Code: Select all

function tile:spawn(x, y, coli, img, width, height)
	table.insert(tilem, {x = x--[[int]], y = y--[[int]], coli = coli--[[bool]], img = img--[[string]], width = width--[[int]], height = height--[[int]]})
end
Then I added a if coli is true function.

Code: Select all

function tilep:coli()
for i,v in ipairs(tilep) do
if v.coli == true then
if char.x >= v.x - 14 and char.x <= v.x + v.width - 14 and char.y >= v.y - 21 and char.y <= v.y + v.height - 21 then
					char.y = v.y - 21
				end
				if char.x >= v.x and char.x <= v.x + v.width and char.y >= v.y - 21 and char.y <= v.y + v.height - 21 then
					char.y = v.y - 21
				end
				if char.x >= v.x - 14 and char.x <= v.x + v.width - 14 and char.y <= v.y + 21 and char.y >= v.y - v.height + 21 then
					char.x = v.x - 14
				end
				if char.x >= v.x - 14 and char.x <= v.x + v.width - 14 and char.y <= v.y and char.y >= v.y - v.height then
					char.x = v.x - 14
				end
			end
end
end
end
I don't get any errors, but collision isn't how I'd like it to be. At some point, when hitting the tile to the left, it works. At others, it moves the player up. Please help me.

Re: Tile collision problem

Posted: Fri Jul 26, 2013 6:33 am
by CaptainMaelstrom
From just glancing, maybe line 7 needs to be:

Code: Select all

if char.x >= v.x -14 and char.x ... 
instead of

Code: Select all

if char.x >= v.x and char.x ...
Hard to say without you uploading a .love for us to look at.