Continuously Scrolling Background

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
User avatar
Mighty Tuna
Prole
Posts: 4
Joined: Tue Apr 09, 2013 12:26 am

Continuously Scrolling Background

Post by Mighty Tuna »

Hi everyone!
This is my first post as a member. Please inform me if I break any rules. (Just in case...)

How could I make a background scroll to the right continuously, with the same image repeating over and over. I also want the movement to start automatically when the game runs.

I am not creating a game right now, but I just want to know how to do this for future reference.

Here is my code. (Not so much code because I am just focusing on the background)

Code: Select all

function love.load()
	bg = love.graphics.newImage("background.png")
end

function love.update(dt)

end

function love.draw()
    love.graphics.setColor(255,255,255,255)
    love.graphics.draw(bg)
end
Thanks for you help.
User avatar
master both
Party member
Posts: 262
Joined: Tue Nov 08, 2011 12:39 am
Location: Chile

Re: Continuously Scrolling Background

Post by master both »

I made this demo by cutting an image in two and resetting the position everytime one of them reach the end.
I know there might be a better solution, but this is what i could think of
Attachments
bg_scroll.love
(5.02 MiB) Downloaded 494 times
User avatar
Mighty Tuna
Prole
Posts: 4
Joined: Tue Apr 09, 2013 12:26 am

Re: Continuously Scrolling Background

Post by Mighty Tuna »

Could you point out the part of the script that would make it scroll in the other direction? Sorry if I didn't make it clear.
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: Continuously Scrolling Background

Post by substitute541 »

There are many ways to do it, but having 2 background images scrolling is the simplest way. The code may look like this (assuming that the background image is the same size as the window, and that you are changing the X coordinate only):

Code: Select all

if bg1.x + bg.width <= 0 then
    local dx = bg1.x + bg.width
    bg1.x = love.graphics.getWidth() - dx
elseif bg1.x >= love.graphics.getWidth() then
    local dx = love.graphics.getWidth() - bg1.x
    bg1.x = -dx
end
Code is untested yet. Do the same thing for bg2 (note that, bg.width is just the width of both backgrounds).
Currently designing themes for WordPress.

Sometimes lurks around the forum.
User avatar
Mighty Tuna
Prole
Posts: 4
Joined: Tue Apr 09, 2013 12:26 am

Re: Continuously Scrolling Background

Post by Mighty Tuna »

Thanks for your help.
This is exactly what I needed.
User avatar
Username
Citizen
Posts: 54
Joined: Mon Jan 28, 2013 1:25 pm

Re: Continuously Scrolling Background

Post by Username »

Mighty Tuna wrote:Hi everyone!
This is my first post as a member. Please inform me if I break any rules. (Just in case...)
I inform you that you broke a basic rule on your first post: search the forums before posting a question, because just 10 threads below this, you can find this:

viewtopic.php?f=4&t=32969
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Continuously Scrolling Background

Post by Robin »

Welcome Mighty Tuna!

If you're worried about breaking the rules here, might I suggest reading the forum rules if you haven't already?
Help us help you: attach a .love.
User avatar
Mighty Tuna
Prole
Posts: 4
Joined: Tue Apr 09, 2013 12:26 am

Re: Continuously Scrolling Background

Post by Mighty Tuna »

Username wrote:
Mighty Tuna wrote:Hi everyone!
This is my first post as a member. Please inform me if I break any rules. (Just in case...)
I inform you that you broke a basic rule on your first post: search the forums before posting a question, because just 10 threads below this, you can find this:

viewtopic.php?f=4&t=32969
I saw that before but I am not using a tiled background. Just 1 or 2 pictures.
User avatar
Username
Citizen
Posts: 54
Joined: Mon Jan 28, 2013 1:25 pm

Re: Continuously Scrolling Background

Post by Username »

Mighty Tuna wrote:
Username wrote:
Mighty Tuna wrote:Hi everyone!
This is my first post as a member. Please inform me if I break any rules. (Just in case...)
I inform you that you broke a basic rule on your first post: search the forums before posting a question, because just 10 threads below this, you can find this:

viewtopic.php?f=4&t=32969
I saw that before but I am not using a tiled background. Just 1 or 2 pictures.
Please, read my last post in that thread because that is what you are asking for.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot], Semrush [Bot] and 9 guests