math.random issue

I am trying to grab a random image from a table of images. Then based on which image is chosen do something else. The problem is that once the random image appears I can not figure out how to identify that image from the others. The following works, but in the onCollide function I have to use “item” and that could be either image. Is there a way to say if the random image chosen is yellow.png then…
Thanks for any help!

[lua]_W = display.contentWidth
_H = display.contentHeight

local rect3 = display.newRect( 0, _H/2+140, 480, 10 )
rect3:setFillColor( 255, 255, 255, 255 )
physics.addBody( rect3, “static”, { isSensor = true } )

local good = 0

local textObject_03 = display.newText( “0”, _W/2, 40, “Courier”, 40 )
textObject_03:setTextColor( 255,255,255 )

myPics = {“images/yellow.png”,“images/red.png” }

local function choose()

local r = math.random ( 1, #myPics )

local item = display.newImageRect ( myPics[r],30,30)
item.x=_W/2
item.y=_H/2

physics.addBody(item, “dynamic”, {bounce = 0})

local function onCollide (self, event)
if event.phase == “began” and event.other == item then
good = good +1
textObject_03.text = good
end
end
rect3.collision = onCollide
rect3:addEventListener(“collision”, rect3)

end

choose()[/lua] [import]uid: 39370 topic_id: 13896 reply_id: 313896[/import]

just set another property for your item with the random number.
eg item.tag = r
and by checking that we can now which item it is.
[lua]local function choose()

local r = math.random ( 1, #myPics )

local item = display.newImageRect ( myPics[r],30,30)
item.x=_W/2
item.y=_H/2
item.tag = r

physics.addBody(item, “dynamic”, {bounce = 0})

local function onCollide (self, event)
if event.phase == “began” and event.other.tag == 1 then
good = good +1
textObject_03.text = good
end
end
rect3.collision = onCollide
rect3:addEventListener(“collision”, rect3)

end[/lua] [import]uid: 71210 topic_id: 13896 reply_id: 51048[/import]

Thank you so much! I totally missed that. [import]uid: 39370 topic_id: 13896 reply_id: 51189[/import]

I have one more question-trying to get the hang of tables. I can setup a table with functions in it but how would I call a random function.Not sure how to write the last line:

[lua]local function test1()
local box1 = display.newImageRect(“images/yellow.png”, 30, 30)
box1:setReferencePoint(display.CenterReferencePoint)
box1.x=_W/2
box1.y=_H/2
end

local function test2()
local box2 = display.newImage(“images/yellow.png”, 30, 30)
box2:setReferencePoint(display.CenterReferencePoint)
box2.x=_W/2
box2.y=_H/2
end

local myPics = {
func1=test1,
func2=test2
}
r = math.random ( 1, #myPics )[/lua]
[import]uid: 39370 topic_id: 13896 reply_id: 51193[/import]

why don’t you do something like this ?
[lua]local _W = display.contentWidth
local _H = display.contentHeight
local function test(randnum)
if randnum == 1 then
local box1 = display.newImageRect(“images/yellow.png”, 30, 30)
box1:setReferencePoint(display.CenterReferencePoint)
box1.x=_W/2
box1.y=_H/2
elseif randnum ==2 then
local box2 = display.newImage(“images/yellow.png”, 30, 30)
box2:setReferencePoint(display.CenterReferencePoint)
box2.x=_W/2
box2.y=_H/2
end
end

local myPics = {“test1”,“test2”}
r = math.random ( 1, #myPics )
test®[/lua] [import]uid: 71210 topic_id: 13896 reply_id: 51209[/import]

if you really want to put it as 2 seperate function you can do this instead…
[lua]local _W =display.contentWidth
local _H =display.contentHeight
local function test1()
local box1 = display.newImageRect(“images/yellow.png”, 30, 30)
box1:setReferencePoint(display.CenterReferencePoint)
box1.x=_W/2
box1.y=_H/2
end

local function test2()
local box2 = display.newImage(“images/yellow.png”, 30, 30)
box2:setReferencePoint(display.CenterReferencePoint)
box2.x=_W/2
box2.y=_H/2
end

function callback (name)
if name == “test1” then
return test1
elseif name == “test2” then
return test2
end
end

local myPics = {“test1”,“test2”}
r = math.random ( 1, #myPics )
fnname = callback(myPics[r])
print(fnname)
fnname()[/lua] [import]uid: 71210 topic_id: 13896 reply_id: 51211[/import]

Thanks-those are great! You have been a huge help. I think I am starting to get the hang of this now! Thanks again. [import]uid: 39370 topic_id: 13896 reply_id: 51212[/import]

I figured out a better way of doing this…
[lua]local _W =display.contentWidth
local _H =display.contentHeight
function test1()
local box1 = display.newImageRect(“images/yellow.png”, 30, 30)
box1:setReferencePoint(display.CenterReferencePoint)
box1.x=_W/2
box1.y=_H/2
end

function test2()
local box2 = display.newImage(“images/yellow.png”, 30, 30)
box2:setReferencePoint(display.CenterReferencePoint)
box2.x=_W/2
box2.y=_H/2
end

function callback (name)
return _G[name]
end

local myPics = {“test1”,“test2”}
r = math.random ( 1, #myPics )
fnname = callback(_G[myPics[r]])()
fnname()[/lua] [import]uid: 71210 topic_id: 13896 reply_id: 51218[/import]