Scrolling background

Hi, I’m new with Corona SDK.

I’m trying make my background with endless scrolling, but never looks right.

I’m using the code below:

–>add background image
local background = display.newImage(“test.png”, true)
background.x = display.contentWidth / 2
background.y = display.contentHeight / 2

local background2 = display.newImage(“test.png”, true)
background2.x = display.contentWidth / 2
background2.y = display.contentHeight / 2

and the function:

local tPrevious = system.getTimer()
local function move(event)
    local tDelta = event.time - tPrevious
    tPrevious = event.time
 
    local xOffset = ( 0.2 * tDelta )

background.x = background.x - xOffset
    background2.x = background2.x - xOffset
 
    if (background.x + background.contentWidth) < 0 then
        background:translate( 610 * 2, 0)
    end
    if (background2.x + background2.contentWidth) < 0 then
        background2:translate( 610 * 2, 0)
    end
end

Runtime:addEventListener( “enterFrame”, move );

Thank you for the help.

Hi alexsantos2102

Try this code. 

local background = display.newImage(“test.png”, true)

background.x = display.contentWidth / 2

background.y = display.contentHeight / 2

local background2 = display.newImage(“test.png”, true)

background2.x = display.contentWidth*1.5

background2.y = display.contentHeight / 2

local tPrevious = system.getTimer()

local function move(event)

    local tDelta = event.time - tPrevious

    tPrevious = event.time

    local xOffset = ( 0.2 * tDelta )

    background.x = background.x - xOffset

    background2.x = background2.x - xOffset

    

    if background.x < -display.contentWidth / 2 then

        background.x = display.contentWidth*1.5

    end

    if background2.x < -display.contentWidth / 2 then

        background2.x = display.contentWidth*1.5

    end

end

Runtime:addEventListener( “enterFrame”, move );

Best Regards,

Team, SP Technolab

www.sptechnolab.com

Perfect!!

it’s working. Thank you so much. :smiley:

Hi alexsantos2102

Try this code. 

local background = display.newImage(“test.png”, true)

background.x = display.contentWidth / 2

background.y = display.contentHeight / 2

local background2 = display.newImage(“test.png”, true)

background2.x = display.contentWidth*1.5

background2.y = display.contentHeight / 2

local tPrevious = system.getTimer()

local function move(event)

    local tDelta = event.time - tPrevious

    tPrevious = event.time

    local xOffset = ( 0.2 * tDelta )

    background.x = background.x - xOffset

    background2.x = background2.x - xOffset

    

    if background.x < -display.contentWidth / 2 then

        background.x = display.contentWidth*1.5

    end

    if background2.x < -display.contentWidth / 2 then

        background2.x = display.contentWidth*1.5

    end

end

Runtime:addEventListener( “enterFrame”, move );

Best Regards,

Team, SP Technolab

www.sptechnolab.com

Perfect!!

it’s working. Thank you so much. :smiley: