Code: Select all
function love.keypressed(key, scancode)
if scancode == "w" then
local obsts = {wall, wall2}
if isGrounded(player, obsts) then
print("Eagle has landed")
player.yVel = -5
end
end
end
You have this code in
love.keypressed(key, scancode) so collision is only checked when the "w" is pressed.
You want to always check collisions.
I moved the isGrounded() check to the end of love.update() and it worked.
(The player bounces upwards when hitting a wall)
/edit:
Ah, I see what you mean. You want collres() and slide() function to handle the collision.
1) Sometimes
obst is used as a table of obstacles and sometimes it is a single object. You mix both.
In slide()
obst is only a single obstacle (you use obst.x) but then you pass that single object to isGrounded()
However isGrounded() wants a table in the form of [1]=wall,[2]=wall2 etc.
(So ipairs can loop through all elements).
But the loop never executes (use a print to check) because instead a single wall/obst is passed.
This works, note the
{obst
} to create a table that isGrounded() can work with.
Code: Select all
function slide(gamer, obst)
if isGrounded(gamer, {obst}) then
gamer.y = obst.y - gamer.w
gamer.yVel = 0
end
For test I moved that code to the top of slide() function.
It works there. But it does not work when placed within the two if's where you originally had it.
(Not sure what is wrong there)
2)
Code: Select all
local sepY = math.abs((gamer.y + gamer.w / 2)-(obst.y + obst.w / 2))
I think you want gamer
.h and obst
.h here?
3) In function collres(...) you have:
Code: Select all
gamer.yVel = gamer.yVel + gravity*dt
So every time you check collision with a wall, gravity is applied one more time to the player.
(One wall: player falls normal. Two walls: player falls twice as fast. 3 walls = tripple fall speed. ...)