Page 1 of 1

Fine tile scrolling with Tiled

Posted: Sun Dec 18, 2011 11:33 am
by qrux
Hey,
i'm using Advanced Tiled Loader to load maps, and it's working great. However, i wanted the map to scroll when i moved and wrote this piece of code:

Code: Select all

  if love.keyboard.isDown("up", "w") then ty = ty + 250*dt 
  end
  if love.keyboard.isDown("right", "d") then tx = tx - 250*dt
  end
  if love.keyboard.isDown("left", "a") then tx = tx + 250*dt
  end
  if love.keyboard.isDown("down", "s") then ty = ty - 250*dt
  end
Unfortunately, that makes the player move one tile at a time :( How can i make it scroll "per pixel" instead? I looked up the "Fine Tile Scrolling" tutorial on the wiki but didn't really understand it.
My .love file is here: http://www.mediafire.com/?o92it7mpgp434ht

Thanks!

Re: Fine tile scrolling with Tiled

Posted: Sun Dec 18, 2011 1:23 pm
by Ellohir
I use a camera and set the center into the player everytime it moves (the player can move one pixel at a time). Also, the camera has bounds on the map end so it doesn't show anything outside the map (the only way the player moves from the center of the screen). I use http://nova-fusion.com/2011/05/09/camer ... nt-bounds/

PS: Why the sound file? You don't play it and it's quite a big file.

Re: Fine tile scrolling with Tiled

Posted: Mon Dec 19, 2011 10:17 pm
by qrux
Ah, i see! Thanks!
Ellohir wrote:PS: Why the sound file? You don't play it and it's quite a big file.
I did use it in the past but i don't anymore, and never got around to deleting it. Done now though :)

Re: Fine tile scrolling with Tiled

Posted: Tue Dec 20, 2011 12:51 am
by Kadoba
Your movement actually is per-pixel. The blocky movement is a combination of love.graphics.scale() and flooring your love.graphics.translate() values. Since your pixels are 4x larger this will makes your screen move 4 pixels at a time. This is easier to see if you slow down your speed a lot. Instead of flooring your translation do this instead:

Code: Select all

local ftx, fty = tx - tx % (1/scale),  ty - ty %( 1/scale)
This is still flooring your values but in such a way that it will always move one pixel on your screen rather than the scaled up pixels.

I've reuploaded your game with the fix. You can change the scale with Q and E.

Re: Fine tile scrolling with Tiled

Posted: Tue Dec 20, 2011 7:36 am
by qrux
Kadoba wrote:Your movement actually is per-pixel. The blocky movement is a combination of love.graphics.scale() and flooring your love.graphics.translate() values. Since your pixels are 4x larger this will makes your screen move 4 pixels at a time. This is easier to see if you slow down your speed a lot. Instead of flooring your translation do this instead:

Code: Select all

local ftx, fty = tx - tx % (1/scale),  ty - ty %( 1/scale)
This is still flooring your values but in such a way that it will always move one pixel on your screen rather than the scaled up pixels.

I've reuploaded your game with the fix. You can change the scale with Q and E.
Thank you for your answer! Although, i have found that it was another problem that was causing this. I noticed the game was running at ~8 FPS, so i checked what was causing that and i found that i did not use Advanced Tiled Loader's

Code: Select all

map:autoDrawRange(tx, ty, scale, 7)
All that does is that it only does draw the map around you, not "infinitely".

However, i have implemented your fix too. Thanks!