I have used rects instead images.
Try (simple method)
local \_W, \_CY = display.contentWidth, display.contentCenterY branch1 = display.newRect(0, 0, 50, 200); branch1.anchorX, branch1.anchorY = 0, 1; branch1:setFillColor(0.1, 0.7, 0.3) branch1.x, branch1.y = 0, \_CY; branch1.speed = 3 branch2 = display.newRect(0, 0, 50, 100); branch2.anchorX, branch2.anchorY = 0, 1; branch2.x, branch2.y = \_W, \_CY; branch2.speed = 3 local function scrollBackground( object ) if( object.x \< -\_W ) then object.x = \_W; else object.x = object.x - object.speed; end end local function enterFrame(event) scrollBackground(branch1) scrollBackground(branch2) end Runtime:addEventListener( "enterFrame", enterFrame );
Try (more complex one)
app.lua
local \_M = {} -- Call f next frame function \_M.nextFrame(f) timer.performWithDelay(1, f) end -- For internal use function \_M.enterFrame() for i = 1, #\_M.enterFrameFunctions do \_M.enterFrameFunctions[i]() end end -- Call f each frame function \_M.eachFrame(f) if not \_M.enterFrameFunctions then \_M.enterFrameFunctions = {} Runtime:addEventListener('enterFrame', \_M.enterFrame) end table.insert(\_M.enterFrameFunctions, f) return f end -- Stop calling f function \_M.eachFrameRemove(f) if not f or not \_M.enterFrameFunctions then return end local ind = table.indexOf(\_M.enterFrameFunctions, f) if ind then table.remove(\_M.enterFrameFunctions, ind) if #\_M.enterFrameFunctions == 0 then Runtime:removeEventListener('enterFrame', \_M.enterFrame) \_M.enterFrameFunctions = nil end end end -- Stop everything function \_M.eachFrameRemoveAll() Runtime:removeEventListener('enterFrame', \_M.enterFrame) \_M.enterFrameFunctions = nil end return \_M
local app = require('lib.app') local \_W, \_CY = display.contentWidth, display.contentCenterY branch1 = display.newRect(0, 0, 100, 300); branch1:setFillColor(0.2, 0.3, 0.4) branch1.anchorX, branch1.anchorY = 0, 1; branch1.x, branch1.y = 0, \_CY; branch1.speed = 3 branch2 = display.newRect(0, 0, 50, 100); branch1:setFillColor(0.1, 0.7, 0.3) branch2.anchorX, branch2.anchorY = 0, 1; branch2.x, branch2.y = 0, \_CY; branch2.speed = 3 function branch1:startScrollBackground() self.fId = app.eachFrame(function() if ( self.x \< -\_W ) then self.x = \_W; else self.x = self.x - self.speed; end end) end function branch1:finalize() app.eachFrameRemove(self.fId) end branch1:addEventListener('finalize') branch1:startScrollBackground() function branch2:startScrollBackground() self.fId = app.eachFrame(function() if ( self.x \< -\_W ) then self.x = \_W; else self.x = self.x - self.speed; end end) end function branch2:finalize() app.eachFrameRemove(self.fId) end branch2:addEventListener('finalize') branch2:startScrollBackground()
Last code is based on tip from spiralcodestudio.com.
Unless you set
display.setDefault( "isAnchorClamped", false )
the anchor points are limited to the range between 0.0 and 1.0 Read more on Tutorial: Extending Anchor Points.
You don’t need use semicolon at end of the line. You have to do this if you write more than one instruction in the same line.
Maybe this link ‘What is faster - many small enterframe listeners’ or only one but large’ would be helpful.