My old app has a number of screens e.g. 1.png, 2.png etc that are selected randomly using the “shake” function and it still works. I got the code from someone on here a few years ago. However, I now want to substitute the ‘shake’ function with a “play” button to randomly select screens but it doesn’t work. I have tried replacing the ‘shake’ function with a ‘button’ function. Any help would be very much appreciated. Here is the original ‘shake’ code on my play.lua screen. Thanks
module(…, package.seeall)
function new()
local localGroup = display.newGroup()
→ This is how we start every single file or “screen” in our folder, except for main.lua
– and director.lua
→ director.lua is NEVER modified, while only one line in main.lua changes, described in that file
local background = display.newImage (“shake.png”)
localGroup:insert(background)
→ This sets the background
local playbutton = display.newImage (playbutton.png")
playbutton.x = 332
playbutton.y = 715
localGroup:insert(playbutton)
local img = { “1.png”, “2.png”, “3.png”, “4.png”, “5.png”, “6.png”, “7.png”,“8.png”,“9.png”,“10.png”,“11.png”,
“12.png”,“13.png”,“14.png”,“15.png”,“16.png”,“17.png”,“18.png”,“19.png”,“20.png”,“21.png”,“22.png”,“23.png”,
“24.png”,“25.png”,“26.png”,“27.png”,“28.png”,“29.png”,“30.png”,“31.png”,“32.png”,“33.png”,“34.png”,“35.png”,
“36.png”,“37.png”,“38.png”,“39.png”,“40.png”,“41.png”,“42.png”,“43.png”,“44.png”,“45.png”,“46.png”,“47.png”,
“48.png”,“49.png”,“50.png”,};
local function onShake(event)
if event.isShake then
math.randomseed ( os.time () )
local r = math.random(1,#img);
local image = display.newImage(img[r], 0, 0);
end
return true;
end
Runtime:addEventListener(“accelerometer”, onShake);
return localGroup
end