Hello! I have a trouble to create clickable links for a simple quiz Can you help me?
I would like to have two buttons A and B, and would like to:
- check which one is clicked
- move to another page (scene), depending A or B (ie go to correct if it is A, go to incorrect if it is B)
So far, I customized this example. But, 2) does not work well yet (ie composer.gotoScene(“dev.correct”)). Can you find an easy solution for this?
The main file (simplified) is as follows, to just show you how a module is called. Two tables are created separately to create two buttons, because they are inside two separate functions:
local buttons = require( "dev.module\_buttons" ) function() local button\_table = {"Button A"} buttons.buttons(sceneGroup, 1, 200, 700, 100, button\_table) end function() local button\_table = {"Button B"} buttons.buttons(sceneGroup, 1, 500, 700, 100, button\_table) end
The following is the called module (“dev.module_buttons”):
local composer = require( "composer" ) local scene = composer.newScene() local M = {} M.buttons = function(sceneGroup, number, x, y, size, button\_table) local stored local rects = {} --Store the name of the button clicked local touchButton = function (event) local obj = event.target stored = obj.id print ("STORED NUMBER: "..stored) --check which button is clicked and move to another scene, but does not work if stored == "Button A" then composer.gotoScene("dev.correct") end if stored == "Button B" then composer.gotoScene("dev.incorrect") end end --Creation of repetitive buttons for a = 1, number, 1 do local r = display.newText( sceneGroup, button\_table[a], 300, 660, native.systemFont, 40 ) r.x = (x \* a) + 600 r.y = y r:addEventListener("tap",touchButton) r.id = button\_table[a] rects[#rects+1] = r end end return M
Thank you!