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)
