Here's my code, with function names nearby:
Code: Select all
.load()
player.colRect = col:addRectangle(player.x, player.y, player.width, player.height)
-- .update(dt)
-- I have to add player.width / 2 for some reason, otherwise the bounding rectangle is drawn not right under the sprite.
player.colRect:moveTo(player.x + player.width / 2, player.y + player.height / 2)
While the collision works, is detected, etc., when the key is held down, the sprite moves a bit into the sprite it's colliding with. It's explainable by the fact that the colliding box isn't moving, however it allows the sprite to move, even though it shouldn't, as it uses the same position variables as the sprite.
Just in case, here's what I do if the shapes collide: player.x = 128 - player.width (128 is the X position of the test sprite)
P.S. I think this can be solved without attaching .love.