calling random functions which are stored in table

Hi guys hello once again.

I am failing again and again to call random functions which are stored in the table.
1.I have created 3 different function to spawn 3 different objects (enemies) with different behaviors, shapes and col etc.

2.Below all this functions i have created one table and stored above three functions in the table fields.

3.Then at the end i have created performWithDelay function which is calling random functions from the above table.

–Here is the problem–

Now the problem is when m relaunching my game in simulator m getting different object each time… but this same object get created in the single run.(throughout the game)

–What i want–
I want to call random functions per second from the table so that every second different kind of enemy will get spawned.

Here is my sample code :

local function spawnred()
local red = display.newImage(…)
bla bla bla

local function spawngreen()
local red = display.newImage(…)
bla bla bla

local function spawnyellow()
local red = display.newImage(…)
bla bla bla

–Creating table to store functions–
ballTable = {spawnred, spawngreen, spawnyellow}

–want to call random functions stored in table on each second–
tmr = timer.performWithDelay(1000, ballTable[math.random(1,3)], -1)

Thanks in advance [import]uid: 83799 topic_id: 25064 reply_id: 325064[/import]

Hi

I had a similar idea and the problem you’re having is your timer delay is calling the random item and you have set it to -1 which is spawning it indefinitely… but it is only calling the one item which is then spawned every second.

The way to combat it is put your display objects/images into a table and then set the function to call the random display object.

forgive the code as i’m on lunch break and unable to test but something like:-

local redImage = blah blah
local greenImage = blah blah
local yellowImage = blah blah
imageTable = {redImage, greenImage, yellowImage}

local function spawnImage()
image = imageTable[math.random(3)]
– now you can use conditional statements to set behaviours according to which image was called –
if image == 1 then
blah blah
elseif image == 2 then
blah blah
elseif image == 3 then
blah blah
end

tmr = timer.performWithdelay(1000, spawnImage, -1)

This should give you an idea… sorry if it’s not exactly right. basically call your random in the spawn function not the timer, you’re on the right lines though.

Good luck

EDIT:

Or thinking about it, why not use what you’ve got, but put the ballTable into a variable for example

spawnBall = ballTable[math.random(3)]

– then call the timer using the spawnBall variable –

tmr = timer.performWithDelay(1000, spawnBall, -1)

:slight_smile: this would appear much quicker for you. [import]uid: 131622 topic_id: 25064 reply_id: 101822[/import]

Thanks tim888

I’ll check this and let you know if any doubts… [import]uid: 83799 topic_id: 25064 reply_id: 101832[/import]

Thanks tim888

I’ll check this and let you know if any doubts… [import]uid: 83799 topic_id: 25064 reply_id: 101834[/import]

I was trying something and i got this solution without creating table. Now m able to call random spawn functions … let me know if this wrong procedure to do get this result.

Here is my full code:

_W = display.contentWidth
_H = display.contentHeight

local function gerRandomxy()
local randomSide = math.random(1, 4)
local xpos,ypos

if randomSide == 1 then
xpos = math.random(0, _W)
ypos = 0
elseif randomSide == 2 then
xpos = 0
ypos = math.random(0, _H)
elseif randomSide == 3 then
xpos = _W
ypos = math.random(0, _H)
elseif randomSide == 4 then
xpos = math.random(0, _W)
ypos = _H
end
return xpos,ypos
end

local function spawnrect ()

local randomRect = math.random(1, 3)

if randomRect == 1 then
local rect1 = display.newRect(0,0,50,50)
rect1:setReferencePoint(display.CenterReferencePoint)
rect1:setFillColor(255,0,0)
rect1.x,rect1.y = gerRandomxy()

elseif randomRect == 2 then
local rect2 = display.newRect(0,0,50,50)
rect2:setReferencePoint(display.CenterReferencePoint)
rect2:setFillColor(0,255,0)
rect2.x,rect2.y = gerRandomxy()

elseif randomRect == 3 then
local rect3 = display.newRect(0,0,50,50)
rect3:setReferencePoint(display.CenterReferencePoint)
rect3:setFillColor(0,0,255)
rect3.x,rect3.y = gerRandomxy()
end
end

tmr = timer.performWithDelay(1000,spawnrect,50)

With this i am able to spawn random objects on all screen borders (got this idea from my different post)
[import]uid: 83799 topic_id: 25064 reply_id: 102036[/import]

Hi again

To be honest there are always lots of ways to accomplish the same result, I have noticed when writing code. So if the way you have done it works, then it’s fine. I may do it differently but this does not mean your way is wrong.

I have not been using corona/lua very long so I am probably not the best person to check your code, somebody else may be able to give you faster or cleaner ideas… but I say pat on the back if you have it working. Keep going as you are!

:slight_smile: [import]uid: 131622 topic_id: 25064 reply_id: 102149[/import]