Image Buttons layout replicating iOS "home" app icon pages

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?

Hi @techpulsesoftware,

The following guide should help you on this topic. Basically, when detecting a tap or touch, you can read the “event.target” in the listener function, which is a reference to the display object that was tapped. So, you can simply assign a property (variable) to each specific image, like “thisImage.imageNumber = 3” and then, when tapped, you just read “event.target.imageNumber” to get the corresponding number or any other custom property you’ve assigned to it.

http://docs.coronalabs.com/guide/events/touchMultitouch/index.html

Hope this helps,

Brent

Take a look at the Business App Sample published on Corona Labs GitHub page. There is a neat Photo Viewer built into that sample. It presents thumbnail images for the photos in the library and when you tap on a thumbnail it brings you to a full page image display. So in your application the icons are the thumbnails in the Business App Sample. I think you can use the code in that app almost as is for your purposes or it should get you very close to where you need to be. Hope this helps.

Hi @techpulsesoftware,

The following guide should help you on this topic. Basically, when detecting a tap or touch, you can read the “event.target” in the listener function, which is a reference to the display object that was tapped. So, you can simply assign a property (variable) to each specific image, like “thisImage.imageNumber = 3” and then, when tapped, you just read “event.target.imageNumber” to get the corresponding number or any other custom property you’ve assigned to it.

http://docs.coronalabs.com/guide/events/touchMultitouch/index.html

Hope this helps,

Brent

Take a look at the Business App Sample published on Corona Labs GitHub page. There is a neat Photo Viewer built into that sample. It presents thumbnail images for the photos in the library and when you tap on a thumbnail it brings you to a full page image display. So in your application the icons are the thumbnails in the Business App Sample. I think you can use the code in that app almost as is for your purposes or it should get you very close to where you need to be. Hope this helps.