Problem in infinite scrolling background

hello everyone!!

I am new to Corona and lua. So i apologize if I am missing something obvious in my code.

I am trying to make an infinite scrolling background by randomly choosing between three different images.

The background is generated by randomly choosing 2 images which are put adjacent to each other and then translated. The code runs fine without errors for a while but after that both images overlap each other.

I put in print statements to check the output on the terminal. And it is seen that after a while the x values of both images become the same.

Here is my lua code:

-- Group to store images local groundBlocks = display.newGroup() groundBlocks: insert(display.newImage("images/ground1.png")) groundBlocks: insert(display.newImage("images/ground2.png")) groundBlocks: insert(display.newImage("images/ground3.png")) -- 2 variables which draw the images local newground1, newground2; newground1=groundBlocks[3] newground2=groundBlocks[2] newground2.x=newground1.contentWidth + (newground1.contentWidth /2) function drawGround() newground1:translate(-3,0) newground2:translate(-3,0) print("newground1.x="..newground1.x) print("newground2.x="..newground2.x) if(newground1.x \< -(display.contentWidth/2)) then print("newground1") newground1 = randomGround() newground1.x = newground1.contentWidth + (newground1.contentWidth /2) end if(newground2.x \< -(display.contentWidth/2)) then print("newground2") newground2 = randomGround() newground2.x = newground2.contentWidth + (newground2.contentWidth /2) end end --returns chosen type function randomGround() groundsel = math.random(3) if(groundsel == 1) then print ("randomground1") return groundBlocks[1] elseif(groundsel == 2) then print ("randomground2") return groundBlocks[2] else print ("randomground3") return groundBlocks[3] end end timer.performWithDelay(1, drawGround, -1)

hi @mayumimohan,

it would seem that at some point the random generator is going to pick an image which is already showing, ie, #2 is still on the stage and the next up selection is #2, so newground1 == newground2. that would explain why the image(s) seem to overlap, when in fact there is only one showing.

there several ways around this, but i think the best method will depend on how this code will evolve.

cheers,
dmc

Hi @dmccuskey

Thank you so much!!  :slight_smile:

I was able to rectify it by putting in a condition to ensure that newground1 and newground 2 are never equal!! 

hi @mayumimohan,

it would seem that at some point the random generator is going to pick an image which is already showing, ie, #2 is still on the stage and the next up selection is #2, so newground1 == newground2. that would explain why the image(s) seem to overlap, when in fact there is only one showing.

there several ways around this, but i think the best method will depend on how this code will evolve.

cheers,
dmc

Hi @dmccuskey

Thank you so much!!  :slight_smile:

I was able to rectify it by putting in a condition to ensure that newground1 and newground 2 are never equal!!