I’m still not quite sure what you mean by 20 random positions. Do you have 20 designated locations, like say a grid of 4x5 blocks and you want your 20 images to randomly fill those spaces or do you want to have 20 images show up and random X, Y locations and the X, Y isn’t fixed?
The later is hyper easy:
local images = {}
for i = 1, 20 do
images[i] = display.newImageRect(string.format("image%02d.png",i), 64, 64)
-- assumes your images are 64x64 pixels in size and they are named
-- with a sequence of numbers. If not, you would have either setup up
-- another array that had your image names or load them one at a time.
images[i].x = math.random(320)
images[i].y = math.random(480) -- assuming vertical orientation
end
If you have 20 spots that you want to put a different image in, then I would setup a table with the locations pre-defined:
local spots = {}
spots[1] = {}
spots[1].x = 32
spots[1].y = 47
spots[2] = {}
spots[2].x = 49
spots[2].y = 104
...
Then you could load your images in like above:
for i = 1, 20 do
images[i] = display.newImageRect(string.format("image%02d.png",i), 64, 64)
end
Then use a table shuffle function like:
local function shuffle(t)
local rand = math.random
assert(t, "table.shuffle() expected a table, got nil")
local iterations = #t
local j
for i = iterations, 2, -1 do
j = rand(i)
t[i], t[j] = t[j], t[i]
end
end
shuffle(images)
for i = 1, 20 do
images[i].x = spots[i].x
images[i].y = spots[i].y
end
[import]uid: 19626 topic_id: 26329 reply_id: 106792[/import]