Randomizing a moving Background

Hi,

I have been working on randomizing a moving background but have run into a problem. I have a external module that feeds in the certain display objects that I then want to change after they have reached a certain negative x value. I will comment in my code below to make it slightly more obvious what I am referring to.

Thanks,
Chris

[lua]-----------------------------------------------------------------------------------------

– main.lua


display.setStatusBar(display.HiddenStatusBar)
local physics = require( “physics” )
physics.start()
–physics.setDrawMode( “hybrid” )

local speed = 4
local num1 = 1 --these control what display object is loaded from external module
local num2 = 2
local num3 = 3
local blocks = display.newGroup()

local dirt = require(“ground”)

local ground1 = dirt.new({num = num1})
ground1.x = 0
local ground2 = dirt.new({num = num2})
ground2.x = 480
local ground3 = dirt.new({num = num3})
ground3.x = 960

blocks:insert(ground1)
blocks:insert(ground2)
blocks:insert(ground3)
function update( event )
updateBackgrounds()
end

function updateBackgrounds()

for a = 1, blocks.numChildren, 1 do
blocks[a].x = blocks[a].x - speed

if (blocks[a].x < -479) then
blocks[a].x = blocks.numChildren * 480 - 480
local num1 = 2 – This should change the value so that a different color object appears
end

end

end

timer.performWithDelay(1, update, -1)
– below is the external module file

module(…, package.seeall);

local physics = require( “physics” )
physics.start()

function new(params)
if (params) then
if (params.num == 1) then

local ground1 = display.newImage(“GroundColorGreen-01.png”)
ground1:setReferencePoint( display.TopLeftReferencePoint )
physics.addBody(ground1, “static”, {density = 1.0, friction = 0, bounce = 0, isSensor = false})
return ground1

end
if (params.num == 2) then

local ground2 = display.newImage(“GroundColorRed-01.png”)
ground2:setReferencePoint( display.TopLeftReferencePoint )
physics.addBody(ground2, “static”, {density = 1.0, friction = 0, bounce = 0, isSensor = false})
return ground2

end
if (params.num == 3) then

local ground3 = display.newImage(“GroundColorBlue-01.png”)
ground3:setReferencePoint( display.TopLeftReferencePoint )
physics.addBody(ground3, “static”, {density = 1.0, friction = 0, bounce = 0, isSensor = false})
return ground3

end
end
end
[import]uid: 126017 topic_id: 23750 reply_id: 323750[/import]

Figured it out thank you to anyone who looked. [import]uid: 126017 topic_id: 23750 reply_id: 95524[/import]

cjbrasino,
Whould you mind placing your solution here, in case anyone else may need help with something similar? Thanks. [import]uid: 7710 topic_id: 23750 reply_id: 95530[/import]

ya sure here it is. Only main.lua changed

[lua]-----------------------------------------------------------------------------------------

– main.lua


display.setStatusBar(display.HiddenStatusBar)
local physics = require( “physics” )
physics.start()
–physics.setDrawMode( “hybrid” )

local speed = 4

local dirt = require(“ground”)

local ground1 = dirt.new({num = 1})
ground1.x = 0
local ground2 = dirt.new({num = 2})
ground2.x = 480
local ground3 = dirt.new({num = 3})
ground3.x = 960
function update( event )
updateBackgrounds()
end

function updateBackgrounds()
ground1.x = ground1.x - speed
ground2.x = ground2.x - speed
ground3.x = ground3.x - speed

if (ground1.x < -479) then
numGen = math.random(3)
ground1 = dirt.new({num = numGen})
ground1.x = 3 * 480 - 480
end

if (ground2.x < -479) then
numGen = math.random(3)
ground2 = dirt.new({num = numGen})
ground2.x = 3 * 480 - 480
end

if (ground3.x < -479) then
numGen = math.random(3)
ground3 = dirt.new({num = numGen})
ground3.x = 3 * 480 - 480
end
end

timer.performWithDelay(1, update, -1) [import]uid: 126017 topic_id: 23750 reply_id: 95542[/import]