Loop background

I would like to create a background scrolling for my game.
But I am stuck on it, how can I tell my background listeners to create another background object when current background reach .y <0? Coz my background is scroll up

Also, I am able to create object, scroll up, but I wonder how can I removeSelf when it reach y<= -50, meanwhile, stop it’s listener

Thanks for the guide, gurus [import]uid: 10373 topic_id: 7960 reply_id: 307960[/import]

Take a look at Beebe Games Ghost vs Monsters. In that example, it has transition from right to left. That should put you on the right track. [import]uid: 11809 topic_id: 7960 reply_id: 28339[/import]

Not exactly what you want, but I think you can get the right direction from it:
http://developer.anscamobile.com/forum/2011/01/19/how-can-i-change-direction-scrolling-background [import]uid: 42078 topic_id: 7960 reply_id: 28360[/import]

Thanks all for providing me the solution.
it DOES worked to loop my background!

but the Ghost & Monster example doesn’t really suitable
Coz if i wanna randomly create diff birds, in different location (different Y coordinates) to fly from right to left?

The background example doesn’t work this way. there should be a Runtime function to spawn a random bird in different y coordinates [import]uid: 10373 topic_id: 7960 reply_id: 28468[/import]

i manage to make the loop spawn object, and remove, kill event listener
here i share the logic :slight_smile:
Thanks for all the code inspirited me!

[code]
require “sprite”
display.setStatusBar( display.HiddenStatusBar )

– fps x delta ~= 1 sec
– if need to spawn every 3 secs, then
local fps = 20
local fDelta = 1000 / fps
local spawnPlatformRate = 1 * fps * fDelta
local function spawnPlatform()
print(‘spawn platform’)

local rect = display.newRect(0, 0, 120, 30)

rect.y = display.contentHeight
rect.custom = 'obj ’ … system.getTimer()

local tPrev = system.getTimer()
function rect:enterFrame(e)
print('in enterframe ’ … self.custom … ’ y ’ … self.y)

local tDelta = e.time - tPrev
tPrev = e.time
self.y = self.y - (0.09 * tDelta)

– remove object if can’t see it
if self.y < -50 then
Runtime:removeEventListener(“enterFrame”, self)
self:removeSelf()
self = nil

end
end
Runtime:addEventListener( “enterFrame”, rect )
end

local tPrev = system.getTimer()
local tSpawnCount = 0
local onEnterFrame = function(e)

local tDelta = e.time - tPrev
tPrev = e.time
– print('on enter frame tDelta = ’ … tDelta … ’ e.time = ’ … e.time … ’ tPrev = ’ … tPrev)

if tSpawnCount >= spawnPlatformRate then
– spawn platform
spawnPlatform()
– reset tSpawnCount
tSpawnCount = 0
else
tSpawnCount = tSpawnCount + tDelta
end

end

Runtime:addEventListener(‘enterFrame’, onEnterFrame)
[/code] [import]uid: 10373 topic_id: 7960 reply_id: 28473[/import]