randoming array for display object

Help me please with random array, every time I try and it always didn’t work and make me dizzy.
I create 3 arrays t1, t2, and t3. t1 array have 3 objects inside and t2 array have 4 objects and t3 array have 5 objects. The problem is how can I achieve the way to random the t1, t2, and t3 objects without disturb the object inside the t1, t2, and t3?
[import]uid: 64917 topic_id: 11486 reply_id: 311486[/import]

Do you want Random objects from the 3 tables or you want to suffle the 3 tables? [import]uid: 48521 topic_id: 11486 reply_id: 41622[/import]

I just want to show the object one by one and randomly, sorry for the missing point [import]uid: 64917 topic_id: 11486 reply_id: 41626[/import]

help please… [import]uid: 64917 topic_id: 11486 reply_id: 41647[/import]

Can you post your code, will try to help then… [import]uid: 48521 topic_id: 11486 reply_id: 41648[/import]

thanks for the reply and this is my code but I still don’t know which part is wrong and can’t random the square, circle and rectangle
[lua]math.randomseed(os.time())

local square = {}
local rectangle = {}
local circle = {}

local myTable = {}

local function createObj()
– square obj
square[1] = display.newRect(250, 200, 80, 80)
square[1]:setFillColor(255, 100, 0)

square[2] = display.newRect(100, 500, 60, 60)
square[2]:setFillColor(55, 200, 0)

square[3] = display.newRect(400, 500, 60, 60)
square[3]:setFillColor(255, 100, 0)

– rect obj
rectangle[1] = display.newRect(250, 200, 150, 80)
rectangle[1]:setFillColor(5, 155, 200)

rectangle[2] = display.newRect(100, 500, 130, 60)
rectangle[2]:setFillColor(5, 155, 200)

rectangle[3] = display.newRect(400, 500, 130, 60)
rectangle[3]:setFillColor(100, 75, 200)

– circle obj
circle[1] = display.newCircle(300, 200, 60)
circle[1]:setFillColor(220, 0, 20)

circle[2] = display.newCircle(150, 500, 40)
circle[2]:setFillColor(220, 0, 20)

circle[3] = display.newCircle(450, 500, 40)
circle[3]:setFillColor(20, 100, 50)

myTable[#myTable + 1] = square[1]
myTable[#myTable + 1] = square[2]
myTable[#myTable + 1] = square[3]

myTable[#myTable + 1] = rectangle[1]
myTable[#myTable + 1] = rectangle[2]
myTable[#myTable + 1] = rectangle[3]

myTable[#myTable + 1] = circle[1]
myTable[#myTable + 1] = circle[2]
myTable[#myTable + 1] = circle[3]

local randomObject = myTable[math.random(1, #myTable)]
end

createObj()[/lua] [import]uid: 64917 topic_id: 11486 reply_id: 41651[/import]

do you want to display randomly chosen object from any of the 3 tables? [import]uid: 48521 topic_id: 11486 reply_id: 41652[/import]

yes chinmay [import]uid: 64917 topic_id: 11486 reply_id: 41653[/import]

and do you want to create other objects? or you just want to create 1 object?
[import]uid: 48521 topic_id: 11486 reply_id: 41655[/import]

and do you want to create other objects? or you just want to create 1 object?
[import]uid: 48521 topic_id: 11486 reply_id: 41656[/import]

just display one table object, if showing square then all the 3 squares is visible. it’s the same with rect and circle. [import]uid: 64917 topic_id: 11486 reply_id: 41658[/import]

try this, is this what you want?

[lua]math.randomseed(os.time())

local square = {}
local rectangle = {}
local circle = {}

local myTable = {}

local function displayElements(tbl, showElements )
for i = 1, #tbl do
tbl[i].isVisible = showElements
end
end

local function createObj()
– square obj
square[1] = display.newRect(250, 200, 80, 80)
square[1]:setFillColor(255, 100, 0)

square[2] = display.newRect(100, 500, 60, 60)
square[2]:setFillColor(55, 200, 0)

square[3] = display.newRect(400, 500, 60, 60)
square[3]:setFillColor(255, 100, 0)

– rect obj
rectangle[1] = display.newRect(250, 200, 150, 80)
rectangle[1]:setFillColor(5, 155, 200)

rectangle[2] = display.newRect(100, 500, 130, 60)
rectangle[2]:setFillColor(5, 155, 200)

rectangle[3] = display.newRect(400, 500, 130, 60)
rectangle[3]:setFillColor(100, 75, 200)

– circle obj
circle[1] = display.newCircle(300, 200, 60)
circle[1]:setFillColor(220, 0, 20)

circle[2] = display.newCircle(150, 500, 40)
circle[2]:setFillColor(220, 0, 20)

circle[3] = display.newCircle(450, 500, 40)
circle[3]:setFillColor(20, 100, 50)

displayElements(square,false)
displayElements(rectangle,false)
displayElements(circle,false)

local r = math.random(1,3)

if r == 1 then
displayElements(square,true)
elseif r == 2 then
displayElements(rectangle,true)
elseif r == 3 then
displayElements(circle,true)
end

– myTable[#myTable + 1] = square[1]
– myTable[#myTable + 1] = square[2]
– myTable[#myTable + 1] = square[3]

– myTable[#myTable + 1] = rectangle[1]
– myTable[#myTable + 1] = rectangle[2]
– myTable[#myTable + 1] = rectangle[3]

– myTable[#myTable + 1] = circle[1]
– myTable[#myTable + 1] = circle[2]
– myTable[#myTable + 1] = circle[3]

– local randomObject = myTable[math.random(1, #myTable)]
end

createObj()[/lua] [import]uid: 48521 topic_id: 11486 reply_id: 41659[/import]

yes that’s it thank you very much chinmay.patil you’re pro and I need to learn more and how about display image is it the same way to approach it?? [import]uid: 64917 topic_id: 11486 reply_id: 41660[/import]

Yes, same approach for any display object table. [import]uid: 48521 topic_id: 11486 reply_id: 41661[/import]

Hi chinmay I tried to make a game that could change level such as level 1, 2, and 3 referring to my rectangle, square, and circle objects.
Level 1 will create object that show the random between rectangle, square, and circle, if ex. the object is square then I need to drag the small square to the top to match the color and then it will change to second level.
The second level will random based on the objects that haven’t come out yet such as rectangle and circle, and the third level only show the last object.
the problem is I couldn’t identify the objects to display in level 1, 2 and 3 and how to remove the random objects if have 3 probabilities??
this is my code:
[lua]math.randomseed(os.time())

local square = {}
local rectangle = {}
local circle = {}

local nextScene = false
local frame = 0
local level = 1

local function displayElements(tbl, showElements )
for i = 1, #tbl do
tbl[i].isVisible = showElements
end
end

local changeLevel = function()
level = level + 1
if level == 2 then
for k, v in pairs(square) do
square:removeSelf()
end
elseif level == 3 then
rectangle:removeSelf()
elseif level == 4 then
circle:removeSelf()
end
nextScene = true
end

local function render(event)
if nextScene == false then
return
end

frame = frame + 1

if frame == 5 then
if level == 1 then
print(“object level 1”)
displayElements(square,false)
displayElements(rectangle,false)
displayElements(circle,false)

local r = math.random(1,3)

if r == 1 then
displayElements(square,true)
elseif r == 2 then
displayElements(rectangle,true)
elseif r == 3 then
displayElements(circle,true)
end

elseif level == 2 then
print(“object level 2”)

elseif level == 3 then
print(“object level 2”)

end
elseif frame == 10 then
frame = 0
nextScene = false
end
end
local function moveObj(event)
local t = event.target
local phase = event.phase
local rightArea = ((t.x > 250) and (t.x < 330) and (t.y > 200) and (t.y < 280))

if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y

elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false

if rightArea then
if square[3] == t then
square[3].isVisible = false
changeLevel()
elseif rectangle[2] == t then
rectangle[2].isVisible = false
elseif circle[2] == t then
circle[2].isVisible = false
end
end

end
end
return true
end

local addListener = function()
square[2]:addEventListener(“touch”, moveObj)
square[3]:addEventListener(“touch”, moveObj)

rectangle[2]:addEventListener(“touch”, moveObj)
rectangle[3]:addEventListener(“touch”, moveObj)

circle[2]:addEventListener(“touch”, moveObj)
circle[3]:addEventListener(“touch”, moveObj)
end

local function createObj()
– square obj
square[1] = display.newRect(250, 200, 80, 80)
square[1]:setFillColor(255, 100, 0)

square[2] = display.newRect(100, 500, 60, 60)
square[2]:setFillColor(55, 200, 0)

square[3] = display.newRect(400, 500, 60, 60)
square[3]:setFillColor(255, 100, 0)

– rect obj
rectangle[1] = display.newRect(250, 200, 150, 80)
rectangle[1]:setFillColor(5, 155, 200)

rectangle[2] = display.newRect(100, 500, 130, 60)
rectangle[2]:setFillColor(5, 155, 200)

rectangle[3] = display.newRect(400, 500, 130, 60)
rectangle[3]:setFillColor(100, 75, 200)

– circle obj
circle[1] = display.newCircle(250, 200, 60)
circle[1]:setFillColor(220, 0, 20)

circle[2] = display.newCircle(150, 500, 40)
circle[2]:setFillColor(220, 0, 20)

circle[3] = display.newCircle(450, 500, 40)
circle[3]:setFillColor(20, 100, 50)

addListener()
nextScene = true
Runtime:addEventListener(“enterFrame”, render)
end

createObj()[/lua]
[import]uid: 64917 topic_id: 11486 reply_id: 41813[/import]

you should store values of r outside your function. Then when in level == 2 , you do a loop until you get r which has not be pulled out before.

See code below

[lua]local r1 , r2

local function render(event)
if nextScene == false then
return
end

frame = frame + 1

if frame == 5 then
if level == 1 then
print(“object level 1”)
displayElements(square,false)
displayElements(rectangle,false)
displayElements(circle,false)

local r = math.random(1,3)

if r == 1 then
displayElements(square,true)
elseif r == 2 then
displayElements(rectangle,true)
elseif r == 3 then
displayElements(circle,true)
end

r1 = r

elseif level == 2 then
print(“object level 2”)
displayElements(square,false)
displayElements(rectangle,false)
displayElements(circle,false)

local r = math.random(1,3)

while r == r1 do
r = math.random(1,3)
end
if r == 1 then
displayElements(square,true)
elseif r == 2 then
displayElements(rectangle,true)
elseif r == 3 then
displayElements(circle,true)
end

r2 = r

elseif level == 3 then
print(“object level 2”)
print(“object level 2”)
displayElements(square,false)
displayElements(rectangle,false)
displayElements(circle,false)

local r = math.random(1,3)

while r == r1 or r == r2 do
r = math.random(1,3)
end
if r == 1 then
displayElements(square,true)
elseif r == 2 then
displayElements(rectangle,true)
elseif r == 3 then
displayElements(circle,true)
end

end
elseif frame == 10 then
frame = 0
nextScene = false
end
end[/lua]
[import]uid: 48521 topic_id: 11486 reply_id: 41819[/import]

Thanks for your huge help chinmay and sorry for my lua knowledge is still shallow and I need to ask a few questions referring to your changing…
why in the second level you still random the 3 objects, should it be just 2 objects??
And how to store the value outside the function?is it the r1 = r already store the value?
How could I jump into second level and delete the object that already appear? [import]uid: 64917 topic_id: 11486 reply_id: 41828[/import]

why in the second level you still random the 3 objects, should it be just 2 objects??
Because you don’t know which objects have been selected in level 1
how to store the value outside the function?is it the r1 = r already store the value?
yes. r1=r is what i meant by storing the value outside scope of function

How could I jump into second level and delete the object that already appear?
Is that’s very thing you are doing in render function [import]uid: 48521 topic_id: 11486 reply_id: 41831[/import]

Thanks for the explanation chinmay, I tried to fix the code and I have problem with the jumping level part.
If the first level is squares object then after drag the right color, it will success to second level but it doesn’t work for the third level.
For the rectangle and circle objects it doesn’t work well from the first level. Is it the problem I encounter is in the changingLevel function or render function??

[lua]local changeLevel = function()
level = level + 1
if level == 2 then
if r1 == r then
r1:removeSelf()
print(“delete level 1”)
end
elseif level == 3 then
if r2 == r then
r2:removeSelf()
print(“delete level 2”)
end
elseif level == 4 then
if r3 == r then
r3:removeSelf()
print(“delete level 3”)
end
end
nextScene = true
end

local function render(event)
if nextScene == false then
return
end

frame = frame + 1

if frame == 5 then
if level == 1 then
print(“object level 1”)
displayElements(square,false)
displayElements(rectangle,false)
displayElements(circle,false)

local r = math.random(1,3)

if r == 1 then
displayElements(square,true)
elseif r == 2 then
displayElements(rectangle,true)
elseif r == 3 then
displayElements(circle,true)
end
r1 = r

elseif level == 2 then
print(“object level 2”)
displayElements(square,false)
displayElements(rectangle,false)
displayElements(circle,false)

local r = math.random(1,3)

while r == r1 do
r = math.random(1,3)
end
if r == 1 then
displayElements(square,true)
elseif r == 2 then
displayElements(rectangle,true)
elseif r == 3 then
displayElements(circle,true)
end
r2 = r

elseif level == 3 then
print(“object level 3”)
displayElements(square,false)
displayElements(rectangle,false)
displayElements(circle,false)

local r = math.random(1,3)

while r == r1 or r == r2 do
r = math.random(1,3)
end
if r == 1 then
displayElements(square,true)
elseif r == 2 then
displayElements(rectangle,true)
elseif r == 3 then
displayElements(circle,true)
end
r3 = r
end

elseif frame == 10 then
frame = 0
nextScene = false
end
end[/lua] [import]uid: 64917 topic_id: 11486 reply_id: 41835[/import]

thanks chinmay for your help finally I can figure it out how to run the game and remove the objects one by one but I still had 1 disturbing problem with removing the touch listener after finish the game, in the corona terminal it always come out the text runtime error – attempt to index ‘?’ (a nil value)
Is it the order of my function create this porblem? and I use the memory usage to check the size of corona’s memory

[lua]math.randomseed(os.time())

local square = {}
local rectangle = {}
local circle = {}

local r1, r2, r3
local nextScene = false
local frame = 0
local level = 1


– checking memory usage

local monitorMem = function()
collectgarbage() – lua’s memory
print("\nMemory Usage: " …collectgarbage(“count”))
end


– random object function

local function displayElements(tbl, showElements )
for i = 1, #tbl do
tbl[i].isVisible = showElements
end
end


– start to drag objects

local function moveObj(event)
local t = event.target
local phase = event.phase
local rightArea = ((t.x > 250) and (t.x < 330) and (t.y > 200) and (t.y < 280))

if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y

elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false

if rightArea then
if square[3] == t then
square[3].isVisible = false
nextScene = true
elseif rectangle[2] == t then
rectangle[2].isVisible = false
nextScene = true
elseif circle[2] == t then
circle[2].isVisible = false
nextScene = true
end
end

end
end
return true
end


– jumping to the next level

local function render(event)
if not nextScene then
return
end

frame = frame + 1

if frame == 5 then
if level == 1 then
print(“object level 1”)
displayElements(square,false)
displayElements(rectangle,false)
displayElements(circle,false)

local r = math.random(1,3)

if r == 1 then
displayElements(square,true)
elseif r == 2 then
displayElements(rectangle,true)
elseif r == 3 then
displayElements(circle,true)
end
r1 = r

elseif level == 2 then
print(“object level 2”)
displayElements(square,false)
displayElements(rectangle,false)
displayElements(circle,false)

local r = math.random(1,3)

while r == r1 do
r = math.random(1,3)
end
if r == 1 then
displayElements(square,true)
elseif r == 2 then
displayElements(rectangle,true)
elseif r == 3 then
displayElements(circle,true)
end
r2 = r

elseif level == 3 then
print(“object level 3”)
displayElements(square,false)
displayElements(rectangle,false)
displayElements(circle,false)

local r = math.random(1,3)

while r == r1 or r == r2 do
r = math.random(1,3)
end
if r == 1 then
displayElements(square,true)
elseif r == 2 then
displayElements(rectangle,true)
elseif r == 3 then
displayElements(circle,true)
end
r3 = r

elseif level == 4 then
print(“finish random and remove objects”)
displayElements(square,false)
displayElements(rectangle,false)
displayElements(circle,false)

local r = math.random(1,3)

while r == r1 or r == r2 or r == r3 do
r = math.random(1,3)
display:remove(r1)
display:remove(r2)
display:remove(r3)

r1 = nil; r2 = nil; r3 = nil
square[1] = nil; square[2] = nil; square[3] = nil
rectangle[1] = nil; rectangle[2] = nil; rectangle[3] = nil
circle[1]= nil; circle[2]= nil; circle[3]= nil

Runtime:removeEventListener(“enterFrame”, render)

square[2]:removeEventListener(“touch”, moveObj)
square[3]:removeEventListener(“touch”, moveObj)

rectangle[2]:removeEventListener(“touch”, moveObj)
rectangle[3]:removeEventListener(“touch”, moveObj)

circle[2]:removeEventListener(“touch”, moveObj)
circle[3]:removeEventListener(“touch”, moveObj)

Runtime:removeEventListener( “enterFrame”, monitorMem )
print(“clean done”)
end
end

elseif frame == 8 then
level = level + 1

elseif frame == 10 then
frame = 0
nextScene = false
end
end

local addListener = function()
square[2]:addEventListener(“touch”, moveObj)
square[3]:addEventListener(“touch”, moveObj)

rectangle[2]:addEventListener(“touch”, moveObj)
rectangle[3]:addEventListener(“touch”, moveObj)

circle[2]:addEventListener(“touch”, moveObj)
circle[3]:addEventListener(“touch”, moveObj)
end


– creating objects

local function createObj()
– square obj
square[1] = display.newRect(250, 200, 80, 80)
square[1]:setFillColor(255, 100, 0)

square[2] = display.newRect(100, 500, 60, 60)
square[2]:setFillColor(55, 200, 0)

square[3] = display.newRect(400, 500, 60, 60)
square[3]:setFillColor(255, 100, 0)

– rect obj
rectangle[1] = display.newRect(220, 200, 150, 80)
rectangle[1]:setFillColor(5, 155, 200)

rectangle[2] = display.newRect(100, 500, 130, 60)
rectangle[2]:setFillColor(5, 155, 200)

rectangle[3] = display.newRect(400, 500, 130, 60)
rectangle[3]:setFillColor(100, 75, 200)

– circle obj
circle[1] = display.newCircle(290, 230, 60)
circle[1]:setFillColor(220, 0, 20)

circle[2] = display.newCircle(150, 500, 40)
circle[2]:setFillColor(220, 0, 20)

circle[3] = display.newCircle(450, 500, 40)
circle[3]:setFillColor(20, 100, 50)

addListener()
nextScene = true
Runtime:addEventListener(“enterFrame”, render)
Runtime:addEventListener( “enterFrame”, monitorMem )
end

createObj() – start the game[/lua] [import]uid: 64917 topic_id: 11486 reply_id: 41869[/import]