只需尝试以下方法。这可能会帮助您:
local letterHolder = {}
local numOfImages = 10
local xPosArray = {}
local yPosArray = {}
local randX,randY = 0,0
local xExists,yExists = 0,0
local function positionImages(i)
randX = math.random(display.contentWidth)
randY = math.random(display.contentHeight)
if(i==1)then
xPosArray[i] = randX
yPosArray[i] = randY
else
xExists = table.indexOf( xPosArray, randX )
yExists = table.indexOf( yPosArray, randY )
if(xExists~=nil and yExists~=nil and xExists==yExists)then
print("Place already occuped")
positionImages(i)
else
print("Can place here")
xPosArray[i] = randX
yPosArray[i] = randY
end
end
end
for i=1,numOfImages do
letterHolder[i] = display.newImageRect("images/myImage.png", 20, 20)
positionImages(i)
end