how do you loop through single images at a time by tapping on a button

Hi,
I have seen how random pictures can be displayed by shaking.

How do you loop through one image at a time by tapping a button?
I’ve picked up pieces but do not know how to use the loop statement

local button = display.newImage(“image_selector_button.png”, 250, 410, 0, 0)
local plate = {“image1.png”, “image2.png”, “image3.png”};

function button:tap( event )
*** how do you loop through those images with each tap? *****
–local dial = display.newImage(“compass_dial2.png”, 5, 125, 0, 0)
end
button:addEventListener( “tap”, button )
Please advice [import]uid: 3482 topic_id: 495 reply_id: 300495[/import]

Hello Bigland,

Here is my solution to your question. The imageIndex variable remembers where we are in the list of images (1, 2, 3…). At each click the current image is removed, the next index is computed and the next image is shown. I hope this helps.

Cheers, Arjan van IJzendoorn

local button = display.newImage("image\_selector\_button.png", 250, 410, 0, 0)  
local plate = {"image1.png", "image2.png", "image3.png"}  
-- Remember the index of the image that is currently being shown.  
local imageIndex = 1  
-- Show the first image.  
local image = display.newImage(plate[imageIndex])  
function button:tap( event )  
 -- Remove current image.  
 display.getCurrentStage().remove(image)  
 -- Compute index of next image.  
 imageIndex = imageIndex + 1  
 -- If we have reached the end, go back to the first.  
 if imageIndex \> #plate then  
 imageIndex = 1  
 end  
 -- Add new image  
 image = display.newImage(plate[imageIndex])  
end  
button:addEventListener( "tap", button )  

[import]uid: 4824 topic_id: 495 reply_id: 969[/import]

Thank you very much. It works like a charm!!!

I didi not read about imageindex anywhere in the documentation that came with the package or did I miss it?

Where can I find more documentation regarding the language syntax? [import]uid: 3482 topic_id: 495 reply_id: 977[/import]

imageindex is not something special. It is just a name I chose for the variable. I could have used i or x or imageNr or anything else.

The language Corona uses is Lua and you can find more information about Lua here:

http://www.lua.org/docs.html

Programming in Lua (first edition) can be browsed for free at http://www.lua.org/pil/
[import]uid: 4824 topic_id: 495 reply_id: 978[/import]