All you have to do is offset the x and y of the draw method from the actual position of the character
heres some math, focus is the "center" of the screen, it doesn't have to be the actual center, but wherever you want the focus to be (wherever you're drawing your character, I'm guessing in your case)
-character.position (in xcoordinates and ycoordinates, not in tiles) + focus
This is assuming you already have "smooth" movement for the character, which it sounds like it is not the case.
Lets make that then shall we... I'll capitalize variables. You'll need a separate variable POSITION (which contains the x and y coordinates for your character, not in tiles but individual pixels) Lets say you want the character to move 1/10th of the tile per frame. Lets make another variable called MOVEQUEUE, this could be set up multiple ways, but lets make it a table set up like this
Code: Select all
MOVEQUEUE = {direction = {x = 0, y = 0},amount = 0}
Once you input the direction, lets use the code you have currently with a few changes
Code: Select all
function love.keypressed(key)
--this if statement holding the others makes sure you're not already moving, that would make some messy business otherwise
if MOVEQUEUE.amount == 0 then
if key == "up" then
MOVEQUEUE.direction = {x= 0,y = -1}
MOVEQUEUE.amount = 10
end
if key == "left" then
MOVEQUEUE.direction = {x= -1,y = 0}
MOVEQUEUE.amount = 10
end
if key == "down" then
MOVEQUEUE.direction = {x= 0,y = 1}
MOVEQUEUE.amount = 10
end
if key == "right" then
MOVEQUEUE.direction = {x= 1,y = 0}
MOVEQUEUE.amount = 10
end
end
end
Another thing that might help your code be more consistent is that the point {0,0} is the upper left hand corner of your screen, adding 1 to y is going down and adding 1 to x is going right. This code will make your character go "backwards" you might need some readjustment to your tile positioning for this to work
Go into the update() method for your character, or if your character is not a separate object then just use your normal update method
Code: Select all
function CHARACTER.update()
if MOVEQUEUE.amount > 0 then
--This will check if the character will be moving in the bounds of your game, you could also check for tile collision here
if xTile + MOVEQUEUE.direction.x > 0 and yTile + MOVEQUEUE.direction.y > 0 and xTile + MOVEQUEUE.direction.x < MAXTILES.x and yTile + MOVEQUEUE.direction.y < MAXTILES.y then
MOVEQUEUE.amount = MOVEQUEUE.amount - 1
POSITION.x = MOVEQUEUE.direction.x + TILESIZE/10
POSITION.y = MOVEQUEUE.direction.y + TILESIZE/10
end
else
if MOVEQUEUE.direction.x ~= 0 or MOVEQUEUE.direction.y ~= 0 then
xTile = xTile + MOVEQUEUE.direction.x
yTile = yTile + MOVEQUEUE.direction.y
MOVEQUEUE.direction = {x = 0, y = 0}
end
end
end
Now you can change the draw method to draw your spritebatch
Code: Select all
--focus is wherever you draw your character
love.graphics.draw(SPRITEBATCH,-POSITION.x+FOCUS.x,-POSITION.y + FOCUS.y)
Hope this helped, might not be 100% correct and like I said might be off for your game due to your inverted tile coordinates, a temporary fix could be in the "MOVEQUEUE" section to change this chunk of code
Code: Select all
if MOVEQUEUE.direction.x ~= 0 or MOVEQUEUE.direction.y ~= 0 then
xTile = xTile + MOVEQUEUE.direction.x
yTile = yTile + MOVEQUEUE.direction.y
MOVEQUEUE.direction = {x = 0, y = 0}
end
to this (changing the sign on the xTile = xTile + MOVEQUEUE.direction.x and the yTile = yTile + MOVEQUEUE.direction.y) but I'd highly recommend changing how you work with your tiles
Code: Select all
if MOVEQUEUE.direction.x ~= 0 or MOVEQUEUE.direction.y ~= 0 then
xTile = xTile - MOVEQUEUE.direction.x
yTile = yTile - MOVEQUEUE.direction.y
MOVEQUEUE.direction = {x = 0, y = 0}
end