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]