Randomised Selection

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

You shouldn’t need to change much at all there, without seeing your attempt at the button function it’s hard to say where you went wrong.
But all you need to do is replace the onShake function with a touch handling function, and your addEventListener call needs to change so it is being called by the button and passing in the touch function:

local function onTouch(event)
	if event.phase == "ended" then
		math.randomseed ( os.time () )
		local r = math.random(1,#img);
		local image = display.newImage(img[r], 0, 0);
	end

	return true;
end

playbutton:addEventListener("touch", onTouch);

PS: If you use the </> button on any code you enter here, it will be formatted as code making it much easier to read.

Hi Alan, thanks for responding so promptly, very much appreciated.

I will try this and let you know. Thanks again

Regards

Bob

Hi Alan,

Fantastic, you are a wizard!!! have a great weekend…I will!

Regards

Bob

Hi O Wise One!

Right, the random thing is sorted, brilliant. How can I get the play button, which is on my play.lua screen to appear on each randomly selected screen so the player can
press it to randomly select another screen. Hope this makes sense!

Regards

Bob

Hi Alan,

I hope you don’t mind me asking for your help again. My app is now working perfectly thanks to you in terms of functionality. However, I am having a problem with the screen size. I have attached a screenshot of the screen on a simulated iPhone x, which is exactly as I want it to look but when I build for iOS and select e.g. Iphone 14 plus, it only fills some of the screen. I am sure I am doing/not doing something very simple. Any help would be appreciated. TIA
Regards Bob


We’ll probably need to see the content={} portion of your config.lua file.

(Also, when you come back with a totally unrelated question, it’s a good idea to start a new thread so that people with a similar problem who are searching the forum will be able to find the solution.)

Hi Colin,
Thanks for getting back to me so promptly. Apologies for not contacting through main forum, as you will see from my posts I am new to this. Here is what I have at the moment that gives me the screens I attached. Thanks again for your help. Bob

Try changing your scale to "zoomEven" and see what happens.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.