how to run function every halfsecond instead of millisecond?
Posted: Thu Jun 05, 2014 1:04 am
So I'm trying to make the player be affected by gravity until the player collides with something while being able to change the direction of the gravity via the arrow keys. As one last condition, the player should not be able to change the gravity while in mid-air.
This is what I have so far:
But the issue is that the code runs every millisecond or less, and I want the movement to be smooth instead of instant. Therefore, I'm wondering how best to approach this concept.
I realize that having a bunch of functions for checking collision like that is really bad design, but I have made no attempt at cleaning up or optimizing the code as of yet, and also it works and I did it by myself so I'm okay with it for now. I'll probably create another function that calls all of them or something like that. Thank you for your help and your time.
Here is my full main.lua if needed: http://pastebin.com/5X1dydqB
This is what I have so far:
Code: Select all
if love.keyboard.isDown("down") then
repeat
p.y = p.y + force
until
CheckCollision1(px,py,pw,ph, x1,y1,w1,h1) or
CheckCollision2(px,py,pw,ph, x2,y2,w2,h2) or
CheckCollision3(px,py,pw,ph, x3,y3,w3,h3) or
CheckCollision4(px,py,pw,ph, x4,y4,w4,h4) or
CheckCollision5(px,py,pw,ph, x5,y5,w5,h5) or
CheckCollision6(px,py,pw,ph, x6,y6,w6,h6) or
CheckCollision7(px,py,pw,ph, x7,y7,w7,h7)
end
if love.keyboard.isDown("up") then
repeat
p.y = p.y - force
until
CheckCollision1(px,py,pw,ph, x1,y1,w1,h1) or
CheckCollision2(px,py,pw,ph, x2,y2,w2,h2) or
CheckCollision3(px,py,pw,ph, x3,y3,w3,h3) or
CheckCollision4(px,py,pw,ph, x4,y4,w4,h4) or
CheckCollision5(px,py,pw,ph, x5,y5,w5,h5) or
CheckCollision6(px,py,pw,ph, x6,y6,w6,h6) or
CheckCollision7(px,py,pw,ph, x7,y7,w7,h7)
end
if love.keyboard.isDown("right") then
repeat
p.x = p.x + force
until
CheckCollision1(px,py,pw,ph, x1,y1,w1,h1) or
CheckCollision2(px,py,pw,ph, x2,y2,w2,h2) or
CheckCollision3(px,py,pw,ph, x3,y3,w3,h3) or
CheckCollision4(px,py,pw,ph, x4,y4,w4,h4) or
CheckCollision5(px,py,pw,ph, x5,y5,w5,h5) or
CheckCollision6(px,py,pw,ph, x6,y6,w6,h6) or
CheckCollision7(px,py,pw,ph, x7,y7,w7,h7)
end
if love.keyboard.isDown("left") then
repeat
p.x = p.x - force
until
CheckCollision1(px,py,pw,ph, x1,y1,w1,h1) or
CheckCollision2(px,py,pw,ph, x2,y2,w2,h2) or
CheckCollision3(px,py,pw,ph, x3,y3,w3,h3) or
CheckCollision4(px,py,pw,ph, x4,y4,w4,h4) or
CheckCollision5(px,py,pw,ph, x5,y5,w5,h5) or
CheckCollision6(px,py,pw,ph, x6,y6,w6,h6) or
CheckCollision7(px,py,pw,ph, x7,y7,w7,h7)
end
But the issue is that the code runs every millisecond or less, and I want the movement to be smooth instead of instant. Therefore, I'm wondering how best to approach this concept.
I realize that having a bunch of functions for checking collision like that is really bad design, but I have made no attempt at cleaning up or optimizing the code as of yet, and also it works and I did it by myself so I'm okay with it for now. I'll probably create another function that calls all of them or something like that. Thank you for your help and your time.
Here is my full main.lua if needed: http://pastebin.com/5X1dydqB