local function moveVertically() background[1].y = background[1].y - speed background[2].y = background[2].y - speed if(background[1].y \< -background[1].contentHeight)then background[1].y = background[1].contentHeight - speed \* 2 elseif(background[2].y \< -background[2].contentHeight)then background[2].y = background[2].contentHeight - speed \* 2 end end background[1] = display.newImageRect( "images/bg1.png", 1125, 375 ) background[2] = display.newImageRect( "images/bg1.png", 1125, 375 ) background[1].anchorY = 1 background[1].anchorX = 0 background[1].x = 0 background[1].y = display.actualContentHeight background[2].anchorY = 1 background[2].anchorX = 0 background[2].x = 0 background[2].y = display.actualContentHeight + 375 Runtime:addEventListener( "enterFrame", move )
This is code from a game I’m building. My game side scrolls, but I converted this to vertically scrolling as a quick test.
You need a function that gets called every time the screen updates. This is the “moveVertically” function. For this to work you have to have two background images. (Remember in my app I’m doing is a side scroller so my images are landscape, you would want yours to be vertical). I load my background image in twice. I center one on the screen and then make the second one fit exactly below or above (depending on which direction you want to scroll).
Once you have the two images in place, the moveVertically function will subtract a value “speed” (my game us using a speed of 1) to each background’s .y value, (or add if you want to go the other direction). The next block of code checks to see if the first background is off the screen. If it is, it re-positions it below (or above) background[2]. Then it checks to see if background[2] is offscreen and repeats the process. Since we’ve already moved the backgrounds, you have to take the speed into consideration.
Finally you need a Runtime “enterFrame” listener to make the moveVertical() function run every frame.
Now this is the simple way. Because the eventFrame event many not fire exactly on 1/30th or 1/60th second intervals, if you’re doing a lot in between frames, your background movement many not be as smooth as you like. It’s that point where you have to start factoring in the frame time which most of the tutorials above include in the code and complicate it.