I am creating an endless runner game… I have designed 15 different kind of obstacles… I want them to generate randomy.;… Can any one help?
— Thanks :))
I am creating an endless runner game… I have designed 15 different kind of obstacles… I want them to generate randomy.;… Can any one help?
— Thanks :))
Simple aproach might be use 15 if/elseif statements
local rand = math.random local obstacleNumber = rand(15) if obstacleNumber == 1 then ... elseif obstacleNumber == 2 then ... elseif obstacleNumber == 3 then ... ... elseif obstacleNumber == 15 then ... end
May be it can be simplify but it depends how you create obstacles.
Might be better to use a table to define all the obstacles. Something like this (obviously the data is nonsense and for example purposes only).
[lua]
— DEFINE OBSTACLES
local o = {}
o[#o+1] = {name = “tree”, image = “tree.png”, minX = 30, maxX = 60, yRatio = 3,
shape = { 0,-37, 37,-10, 23,34, -23,34, -37,-10 }, density = 3, friction = 0.8, bounce = 0.3}
o[#o+1] = {name = “car”, image = “car.png”, minX = 100, maxX = 150, yRatio = 0.5,
shape = { 0,-37, 37,-10, 23,34, -23,34, -37,-10 }, density = 4, friction = 1.2, bounce = 0.2}
– SPAWN CODE
local ch = math.random(1, #o) – choose a random obstacle id
local ob = o[ch] – get the data relating to this obstacle
local xs = math.random(ob.minX, ob.maxX) – decide how big the obstacle should be
local i = display.newImageRect(ob.image, xs, xs * ob.yRatio) – load the appropriate image at chosen size
physics.addBody(i, {density = ob.density, friction = ob.friction, bounce = ob.bounce, shape = ob.shape}) – add physics body
i.name = ob.name – set the obstacle type so it can be managed within collision listeners etc.
[/lua]
Simple aproach might be use 15 if/elseif statements
local rand = math.random local obstacleNumber = rand(15) if obstacleNumber == 1 then ... elseif obstacleNumber == 2 then ... elseif obstacleNumber == 3 then ... ... elseif obstacleNumber == 15 then ... end
May be it can be simplify but it depends how you create obstacles.
Might be better to use a table to define all the obstacles. Something like this (obviously the data is nonsense and for example purposes only).
[lua]
— DEFINE OBSTACLES
local o = {}
o[#o+1] = {name = “tree”, image = “tree.png”, minX = 30, maxX = 60, yRatio = 3,
shape = { 0,-37, 37,-10, 23,34, -23,34, -37,-10 }, density = 3, friction = 0.8, bounce = 0.3}
o[#o+1] = {name = “car”, image = “car.png”, minX = 100, maxX = 150, yRatio = 0.5,
shape = { 0,-37, 37,-10, 23,34, -23,34, -37,-10 }, density = 4, friction = 1.2, bounce = 0.2}
– SPAWN CODE
local ch = math.random(1, #o) – choose a random obstacle id
local ob = o[ch] – get the data relating to this obstacle
local xs = math.random(ob.minX, ob.maxX) – decide how big the obstacle should be
local i = display.newImageRect(ob.image, xs, xs * ob.yRatio) – load the appropriate image at chosen size
physics.addBody(i, {density = ob.density, friction = ob.friction, bounce = ob.bounce, shape = ob.shape}) – add physics body
i.name = ob.name – set the obstacle type so it can be managed within collision listeners etc.
[/lua]