It's good that you posted a love-file. The problem is not in the snipped you posted, but in the code around. If I get it correctly, then you don't have a proper list of spikes. In the spikes:draw you loop over the pixels of the level and draw the corresponding images, if the pixel has the correct color.
For the collision to work you need a table with all spikes. The level-image should only be read once in the beginning.
Here is a rough idea how to do this. When the game is started you loop over the level-image and for each spike you add an entry into a table called spikeList:
Code: Select all
function buildMap()
spikeList = {}
for x = 0 , world.width-1 do
for y = 0 , world.height-1 do
red,green,blue,alpha = world.img:getPixel(x,y)
if red == 81 and green == 116 and blue == 116 then
table.insert(spikeList,{x=x*9.2*spikes.scale, y = y*9.2*spikes.scale, w = 10, h = 10}
end
end
end
end
Now you have a table. The entries of this table are tables itself, which contain coordinates and height and width.
Next you can do you collision check as you already have it.
Note that in the current version, the collision check is not even called once, because the operator #spikes return 0.