I need to make a scrolling background for my game. It needs to scroll downward, and I have a seamless 320 x 480 picture that I want to use. How would I go about doing this? Thanks.
Try loading your background twice to create two display objects with the same graphic and place one beneath the other.
Then use an enterFrame event to move the background.
Something like this:
-- -- main.lua -- local b1 = display.newImageRect("background.png", 320, 480); b1.anchorX, b1.anchorY = 0, 0; local b2 = display.newImageRect("background.png", 320, 480); b2.anchorX, b2.anchorY = 0, 0; b2.y = b1.height; local previousTimer = 0; local totalTime = 10000; -- scrolling takes 10s from bottom to top local onEnterFrame = function(event) local currentTimer = system.getTimer(); local distance = b1.height / totalTime \* (currentTimer - previousTimer); b1.y = b1.y - distance; b2.y = b2.y - distance; if (b1.y \< -b1.height) then b1.y = 0; b2.y = b1.height; end previousTimer = currentTimer; end Runtime:addEventListener("enterFrame", onEnterFrame);
Thank you so much! I have never used the anchors before.
Try loading your background twice to create two display objects with the same graphic and place one beneath the other.
Then use an enterFrame event to move the background.
Something like this:
-- -- main.lua -- local b1 = display.newImageRect("background.png", 320, 480); b1.anchorX, b1.anchorY = 0, 0; local b2 = display.newImageRect("background.png", 320, 480); b2.anchorX, b2.anchorY = 0, 0; b2.y = b1.height; local previousTimer = 0; local totalTime = 10000; -- scrolling takes 10s from bottom to top local onEnterFrame = function(event) local currentTimer = system.getTimer(); local distance = b1.height / totalTime \* (currentTimer - previousTimer); b1.y = b1.y - distance; b2.y = b2.y - distance; if (b1.y \< -b1.height) then b1.y = 0; b2.y = b1.height; end previousTimer = currentTimer; end Runtime:addEventListener("enterFrame", onEnterFrame);
Thank you so much! I have never used the anchors before.