Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save duncanmcdougall/1581870 to your computer and use it in GitHub Desktop.
Save duncanmcdougall/1581870 to your computer and use it in GitHub Desktop.
Corona Infinite Scroll Background Tile
-- I'm using this code in an survival game similar to one of those high speed motor way games
local backgroundTiles = {}
-- drawBackground()
-- The same 480x320 image is used twice
local drawBackground = function()
backgroundTiles[0] = display.newImageRect( "images/background.jpg", display.contentWidth, display.contentHeight)
backgroundTiles[0].x = display.contentCenterX
backgroundTiles[0].y = display.contentCenterY
localGroup:insert(backgroundTiles[0])
backgroundTiles[1] = display.newImageRect( "images/background.jpg", display.contentWidth, display.contentHeight)
backgroundTiles[1].x = display.contentCenterX
backgroundTiles[1].y = display.contentCenterY - backgroundTiles[1].contentHeight
localGroup:insert(backgroundTiles[1])
end
-- shift the tiles back to starting position when off the screen
local moveBackground = function()
for i=0, 1, 1 do
backgroundTiles[i].y = backgroundTiles[i].y + gameMoveSpeed
if backgroundTiles[i].y - backgroundTiles[i].contentHeight/2 >= display.contentHeight then
backgroundTiles[i].y = 0 - backgroundTiles[i].contentHeight/2
end
end
end
-- gameLoop()
local gameLoop = function()
moveBackground()
end
-- gameInit()
-- Initialise the game and run all the creator functions and add listeners
local gameInit = function()
drawBackground()
Runtime:addEventListener( "enterFrame", gameLoop )
end
@AkashJoshi
Copy link

Took some time to convert it to horizontal scroll.. and I know someday someone else will want it horizontal so here is the code..

sW = display.contentWidth
sH = display.contentHeight
gameMoveSpeed = 8

-- drawBackground()
local drawBackground = function()
-- for first image
backgroundTiles[0] = display.newImageRect( "img/bg1.png", sW, sH)
backgroundTiles[0].x, backgroundTiles[0].y = 0,0
backgroundTiles[0].anchorX, backgroundTiles[0].anchorY = 0,0

-- for rest of the images
    for i=1, 2, 1 do
        backgroundTiles[i] = display.newImageRect( "img/bg"..i..".png", sW, sH)
        backgroundTiles[i].x, backgroundTiles[i].y = backgroundTiles[i-1].contentWidth,0
        backgroundTiles[i].anchorX, backgroundTiles[i].anchorY = 0,0
    end
end

end

-- shift the tiles back to starting position when off the screen
local moveBackground = function()
for i=0, 2, 1 do
backgroundTiles[i].x = backgroundTiles[i].x - gameMoveSpeed
if backgroundTiles[i].x == 0 - display.contentWidth then
backgroundTiles[i].x = display.contentWidth
end
end
end

-- gameLoop()
local gameLoop = function()
moveBackground()
end

-- Initialise the game and run all the creator functions and add listeners
local gameInit = function()
drawBackground()
Runtime:addEventListener( "enterFrame", gameLoop )
end

gameInit()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment