I need to display images so that they mimic the layout of iOS app icons on the various “home” pages. I have the code written to display the various images; however, I cannot figure out how to return a value which corresponds with each individual image on a click event. So basically, when a user clicks an image, an alert should popup and say: "You clicked: "…imageNumber. The problem is, how do I get the value of the clicked image (i.e. imageNumber)? The image file links will be in the form of a json table, so the number of images is unknown.
Here’s my code so far:
plates = { "fat-burning-foods.jpg", "fresh\_food.jpg", "fat-burning-foods.jpg", "star.png", "fat-burning-foods.jpg", "star.png", "fat-burning-foods.jpg", "fresh\_food.jpg", "fat-burning-foods.jpg", "star.png", "fat-burning-foods.jpg", "fresh\_food.jpg", "fat-burning-foods.jpg", "star.png" } -- The PlateId will have corosponding indexes with the plates array. It will be used to query plate information plateId = {1,2,3,4} plateIdRef = {} index = 1 platesIsh = {1,2,3,4,5,6,7,8} local anX = 20 local anY = 120 for i=1, #plates do local bufferY = 20 if index == 4 then bufferY = 110 anX = 20 elseif index \> 4 then bufferY = 110 end if index == 7 then bufferY = 200 anX = 20 elseif index \> 7 then bufferY = 200 if index == 10 then bufferY = 290 anX = 20 elseif index \> 10 then bufferY = 290 end end local dummyVar = math.random() dummyVar = display.newImageRect(plates[index],80, 80) sceneGroup:insert(dummyVar) dummyVar.x = anX + 30 dummyVar.y = anY + bufferY table.insert(plateIdRef, index) function dummyVar:touch( event ) if event.phase == "began" then local alert = native.showAlert( "Corona", event.target, { "OK", "Learn More" } ) --print( "You touched "..dummyVar) return true end end dummyVar:addEventListener( "touch", dummyVar ) anX = anX + 110 index = index + 1 end
Any ideas?