PiotrT
June 15, 2011, 8:16am
1
is it possible ?
objectPosition = { (120,230) , (232,340) , (34,340) } something like this ?
I need do math.random on my objects positions…
Or maybe…
objectPositions = {}
object = {}
object.x = 100
object.y =200
table.insert(objectPositions, object)
any better idea ?
[import]uid: 13156 topic_id: 11262 reply_id: 311262[/import]
This should work:
[code]
math.randomseed( os.time() )
local objectPositions = {}
objectPositions[#objectPositions + 1] = { x = 10, y = 50 }
objectPositions[#objectPositions + 1] = { x = 250, y = 10 }
objectPositions[#objectPositions + 1] = { x = 16, y = 3 }
objectPositions[#objectPositions + 1] = { x = 67, y = 548 }
objectPositions[#objectPositions + 1] = { x = 34, y = 193 }
objectPositions[#objectPositions + 1] = { x = 54, y = 305 }
local randomPosition = objectPositions[math.random( 1, #objectPositions )]
print( "Random X: … randomPosition.x … " - Random Y: " … randomPosition.y )
[/code] [import]uid: 5833 topic_id: 11262 reply_id: 40842[/import]
PiotrT
June 15, 2011, 8:42am
3
cool thank you… AGAIN [import]uid: 13156 topic_id: 11262 reply_id: 40843[/import]
No worries [import]uid: 5833 topic_id: 11262 reply_id: 40844[/import]
PiotrT
June 21, 2011, 2:47am
5
Hi GrahamRanson… what do you think… is there a better way to pick up 10 different and random numbers ? Can you take a look if you will have a second.
getNumber = function()
local newNumbers = {}
local number
for i=1,10 do
number = math.random(1,90)
table.insert(newNumbers, i , number)
if number == newNumbers[i] then
newNumbers[i] = math.random(1,90)
end
end
table.sort(newNumbers)
print (table.concat(newNumbers, ", "))
end
Thank you for your time.
but… I thought that this code is working but is not [import]uid: 13156 topic_id: 11262 reply_id: 41645[/import]
This should work:
[code]
math.randomseed( os.time() )
local getRandomNumbers = function( count, allowDuplicates, range )
local numbers = {}
while #numbers < count do
local number = math.random( range.min, range.max )
local numberCanBeAdded = true
if not allowDuplicates then
for j = 1, #numbers , 1 do
if numbers[j] == number then
numberCanBeAdded = false
break
end
end
end
if numberCanBeAdded then
numbers[#numbers + 1] = number
end
end
return numbers
end
– Ten unique numbers between 1 and 90
local numbers = getRandomNumbers( 10, false, { min = 1, max = 90 } )
for i = 1, #numbers , 1 do
print( numbers[i] )
end
[/code] [import]uid: 5833 topic_id: 11262 reply_id: 41692[/import]
PiotrT
June 22, 2011, 2:13am
7
Thank you GrahamRanson…
That day will came and I will help you too. promise: ) [import]uid: 13156 topic_id: 11262 reply_id: 41861[/import]
I’m sure it will and I look forward to it [import]uid: 5833 topic_id: 11262 reply_id: 41884[/import]
Wazzup
August 3, 2011, 9:10am
9
Graham, is there any chance you could explain how to use the above code?
I’m after something similar (spawning objects with random pre-set positions) but can’t seem to get it to work.
Thanks [import]uid: 40538 topic_id: 11262 reply_id: 48606[/import]
All that code does is get a list of random numbers. You could do something like this to get random positions:
[code]
math.randomseed( os.time() )
local getRandomPosition = function()
local x = math.random( display.contentWidth )
local y = math.random( display.contentHeight )
return x, y
end
local posX1, posY1 = getRandomPosition()
local posX2, posY2 = getRandomPosition()
[/code] [import]uid: 5833 topic_id: 11262 reply_id: 48685[/import]