Reputation: 145
I have a set of pre-declared values to set specific rotations for an object.
local rotations = {900,-900}
And want my spawn function for the blocks to randomly pick one or the other from this function:
local blocks = {}
timerSrc = timer.performWithDelay(1200, createBlock, -1)
function createBlock(event)
b = display.newImageRect("images/block8.png", 20, 150)
b.x = 500
b.y = math.random(100,250)
b.name = 'block'
physics.addBody(b, "static")
transition.to( b, { rotation = math.random(rotations), time = math.random(2700,3700)} )
blocks:insert(b)
end
When I use:
rotation = math.random(-900,900)
it just chooses any values between the 2 numbers rather than 1 or the other. How can I do this correctly ?
Upvotes: 2
Views: 797
Reputation: 122383
If m
is an integer value, math.random(m)
returns integers in range [1, m] randomly. So math.random(2)
returns integers 1
or 2
randomly.
To generate random numbers either 900
or -900
, use:
rotation = math.random(2) == 1 and 900 or -900
Upvotes: 4