This is a simple method i made for a scrolling function. It is used in my Halo Genocide Game
So lets start.First of all we will load the images we will be using for our backgrounds. I use 1 480x272 image for this.Background = Image.load("Background.png")
The next step is to set up our background variables. (note that the 3 background variables use the same image (saves memory)Background_A = {} ---the first scrolling background (positioned as the first image you see)
Background_A.x = 0
Background_A.y = 0
Background_A.Img = Background_Image
Background_B = {} --- the second scrolling background (positioned directly after Background_A)
Background_B.x = 480
Background_B.y = 0
Background_B.Img = Background_Image
Background_C = {} ---the third scrolling background (positioned directly before Background_A)
Background_C.x = - 480
Background_C.y = 0
Background_C.Img = Background_Image
Now thats done lets move onto the code that will make this scroll.function Scroll()
if pad:right() then
Background_A.x = Background_A.x - 4
Background_B.x = Background_B.x - 4
Background_C.x = Background_A.x - 480
end
if pad:left() then
Background_A.x = Background_A.x + 4
Background_B.x = Background_B.x + 4
Background_C.x = Background_C.x + 4
end
if Background_A.x <= -480 then
Background_A.x = 0
end
if Background_B.x <= 0 then
Background_B.x = 480
end
if Background_C.x >= 0 then
Background_C.x = -480
Background_A.x = 0
Background_B.x = 480
end
end
The way this works, is if you press either left or right, the background starts to scroll in the opposite direction.
The code underneath that, basically resets the backgrounds to there original positions so that it should, in theory, scroll indefinitely.
Now we start our main loopwhile true do
screen:clear() ---stops the background from getting garbled
pad = Controls.read() --read pad input
Now to call our scrolling functionScroll() ---call scrolling code
Now to blit our backgrounds (note that we only blit them when needed to prevent slowdown)if Background_A.x <=480 then
screen:blit(Background_A.x, Background_A.y, Background_A.Img)
end
if Background_B.x <=480 then
screen:blit(Background_B.x, Background_B.y, Background_B.Img)
end
if Background_C.x <=480 then
screen:blit(Background_C.x, Background_C.y, Background_C.Img)
end
Now to wrap it all upscreen.waitVblankStart()
screen.flip()
end
Hope that helps a few people get started with scrolling. You can test that with any png image. Just make the image 480x272 and name it "Background.png"
The whole code is below :Background = Image.load("Background.png")
Background_A = {} ---the first scrolling background (positioned as the first image you see)
Background_A.x = 0
Background_A.y = 0
Background_A.Img = Background_Image
Background_B = {} --- the second scrolling background (positioned directly after Background_A)
Background_B.x = 480
Background_B.y = 0
Background_B.Img = Background_Image
Background_C = {} ---the third scrolling background (positioned directly before Background_A)
Background_C.x = - 480
Background_C.y = 0
Background_C.Img = Background_Image
function Scroll()
if pad:right() then
Background_A.x = Background_A.x - 4
Background_B.x = Background_B.x - 4
Background_C.x = Background_A.x - 480
end
if pad:left() then
Background_A.x = Background_A.x + 4
Background_B.x = Background_B.x + 4
Background_C.x = Background_C.x + 4
end
if Background_A.x <= -480 then
Background_A.x = 0
end
if Background_B.x <= 0 then
Background_B.x = 480
end
if Background_C.x >= 0 then
Background_C.x = -480
Background_A.x = 0
Background_B.x = 480
end
end
while true do
screen:clear() ---stops the background from getting garbled
pad = Controls.read() --read pad input
Scroll() ---call scrolling code
if Background_A.x <=480 then
screen:blit(Background_A.x, Background_A.y, Background_A.Img)
end
if Background_B.x <=480 then
screen:blit(Background_B.x, Background_B.y, Background_B.Img)
end
if Background_C.x <=480 then
screen:blit(Background_C.x, Background_C.y, Background_C.Img)
end
screen.waitVblankStart()
screen.flip()
end