[STI] How to add map scrolling?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
ZacBytes
Prole
Posts: 1
Joined: Wed Nov 20, 2019 3:15 am

[STI] How to add map scrolling?

Post by ZacBytes »

Hi guys, I'm working on a top-down 2d game. I need the camera to follow WASD controls and also zoom in and out, but I'm not sure how to do that. I am using STI to draw the tiled map. Can anyone help me out?

Thanks! :)
nequals30
Prole
Posts: 9
Joined: Sun Sep 22, 2019 5:54 pm

Re: [STI] How to add map scrolling?

Post by nequals30 »

This tutorial for STI shows how to make the camera follow a player (see the draw section at the bottom).

The tutorial is a bit outdated (see this comment by Karai17). The right way to do it is essentially something like this:

Code: Select all

function love.draw()
	-- Scale world
	local scale = 2
	local screen_width  = love.graphics.getWidth()  / scale
	local screen_height = love.graphics.getHeight() / scale

	-- Translate world so that player is always centred
	local tx = math.floor(player.x - screen_width  / 2)
	local ty = math.floor(player.y - screen_height / 2)

	-- Draw (different from tutorial)
        map:draw(-tx, -ty, scale, scale)
end
Would make the camera centered on player.x and player.y. Then you could change the 'scale' variable to zoom in and out.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Semrush [Bot] and 3 guests