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]
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