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]