How to create Infinite Scrolling background

Hi everyone,

I dont know how to add the scrolling background (vertical direction)

I have this code

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

but It can only works with V1 how to make a scrolling background with V2

Thanks

[lua]

local bg1 = display.newImageRect(“stars3.png”, xWidth, yHeight)

bg1.x = xmid

bg1.y = ymid

local bg2 = display.newImageRect(“stars3.png”, xWidth, yHeight)

bg2.x = xmid

bg2.y = ymid - display.contentHeight

scroll = 2

local function bgScroll (event)

    bg1.y = bg1.y + scroll

    bg2.y = bg2.y + scroll

    if bg1.y == display.contentHeight * 1.5 then

        bg1.y = display.contentHeight * -.5

    end

    if bg2.y == display.contentHeight * 1.5 then

        bg2.y = display.contentHeight * -.5

    end

end

Runtime:addEventListener(“enterFrame”, bgScroll)

[/lua]

That’s the code I have for my game.  Just modify what image you want to use and the scroll speed and it should nestle in nicely.

Thanks for sharing… your code working well …

[lua]

local bg1 = display.newImageRect(“stars3.png”, xWidth, yHeight)

bg1.x = xmid

bg1.y = ymid

local bg2 = display.newImageRect(“stars3.png”, xWidth, yHeight)

bg2.x = xmid

bg2.y = ymid - display.contentHeight

scroll = 2

local function bgScroll (event)

    bg1.y = bg1.y + scroll

    bg2.y = bg2.y + scroll

    if bg1.y == display.contentHeight * 1.5 then

        bg1.y = display.contentHeight * -.5

    end

    if bg2.y == display.contentHeight * 1.5 then

        bg2.y = display.contentHeight * -.5

    end

end

Runtime:addEventListener(“enterFrame”, bgScroll)

[/lua]

That’s the code I have for my game.  Just modify what image you want to use and the scroll speed and it should nestle in nicely.

Thanks for sharing… your code working well …