Hi,
The first thing that came to mind is that you could move the player right whenever the "right" key is tapped - but I assume you want to keep pressing the button and want the player to move at a constant speed?
If you want "jumping" movement, this is how I'd do it: Just modify player.draw:
Code: Select all
function player.draw()
local drawX, drawY
drawX = math.floor( player.x /cursorW ) * cursorW
drawY = math.floor( player.y /cursorH ) * cursorH
love.graphics.draw( cursor, drawX, drawY )
end
I assumed that cursorW and cursorH are the sizes of the steps you want the cursor to take.
Internally, the player.x and player.y are still the continuous position, not the jumping position. You'll need to keep them, but maybe rename them and calculate the player.x and player.y in player.update(), after calling player.physics and player.move...?
So something like this:
Code: Select all
function player.update( dt )
player.move( dt )
player.physics( dt )
player.jumpX = math.floor( player.x /cursorW)*cursorW
player.jumpY = math.floor( player.y /cursorH)*cursorH
end
function player.draw()
love.graphics.draw( cursor, player.jumpX, player.jumpY )
end
Now, whenever you need to check where the player actually is (collision detection etc) simply check player.jumpX and jumpY (might want to rename those to something more useful).
Two more things:
I remembered that the "jumping" coordinates could also be calculated with:
x = player.x - player.x % cursorW
y = player.y - player.y % cursorH
I
think that's a bit faster because there's one less multiplication there. The % (modulo) operator gives the "reminder" when dividing a number by another number, so subtracting that reminder from the original number will also be like rounding down to the nearest multiple of cursorW or cursorH.
But you can use either way.
Also, you use the keyword "local" at the beginning - I believe the way you use it, only cursor is made local. You'd need to prepend all the lines with a "local" in order to make the other variables local - if that's what you want to do.