I want to create an endless terrain that randomly changes at run time. The width of the screen is 480px. I use 8 pictures 240 px width which i want them to randomly change.That is to say, 2 of the 8 images will be shown moving, at same time, on the screen and when one of them gets out of the screen, it will be randomly replaced with one of 6 rest.
that is what i have done until now:
[code]
------------------ the imgs var:
local terrain1 = display.newImage("image/ground/ground1.png") -- add img to var terrain1
... -- etc.
----------------- add imgs on table:
local ground = { -- table name
{ img = terrain1, move = true }, -- add img var and bool var (to give permision to move or not)
{ img = terrain2, move = true }, -- etc.
...
}
------------------ the function:
function terrainMove()
for a = 1, #ground, 1 do -- for all img on the table
if((ground[a].img.x) \< -120) then -- if the position of img is outside screen (img with = 240)
(ground[a].img.x ) = 600 -- sets img to 600px
ground[a].move = false -- set move var to false in order be posible to be chosen
if (ground[a].move) == false then -- if var is false then
local num = math.random ( 1, 8 ) -- the random number
ground[num].move= true -- choose img and set var to true to be used latter
end
if (ground[a].move) == true then -- if var is true
(ground[a].img):translate(speed \* -1, 0) -- move the img
ground[a].move = true -- while is moving set move var to false in order not be posible to be chosen
end
end
timer.performWithDelay(1,terrainMove, -1) -- call function
[/code]
If i use the choise from another example in the forum:
ground[#ground] = display.newImage( groupTerrain, "image/ground"..num..".png", ground[#ground-1].x + ground[#ground-1].contentWidth/2, 200 )
shall be a delay while loading img from file every time?
Thanx!
[import]uid: 182030 topic_id: 33959 reply_id: 333959[/import]