Created
January 9, 2012 08:18
-
-
Save duncanmcdougall/1581870 to your computer and use it in GitHub Desktop.
Corona Infinite Scroll Background Tile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
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()