Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to create clickable links for a simple quiz?
Started by GO4IT May 13 2019 01:27 AM

3 replies to this topic
quiz clickable link gotoscene

Best Answer SGS , 13 May 2019 - 01:40 AM

Keep it simple...

local function onTap(event)
  if event.target.isCorrect then
    composer.gotoScene("my correct scene")
  else composer.gotoScene("my incorrect scene") end
end


local button1 = display.newText( sceneGroup, "correct", 200, 50, native.systemFont, 40 )
button1:translate(200, 700)
button1:addEventListener("tap", onTap)
button1.isCorrect = true

local button2 = display.newText( sceneGroup, "incorrect", 200, 50, native.systemFont, 40 )
button2:translate(500, 700)
button2:addEventListener("tap", onTap)


[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

GO4IT

[GLOBAL: userInfoPane.html]
GO4IT
  • Observer

  • 13 posts
  • Corona SDK

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:

  1. check which one is clicked
  2. 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!



[TOPIC: post.html]
#2

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 2,024 posts
  • Corona SDK

  Best Answer

Keep it simple...

local function onTap(event)
  if event.target.isCorrect then
    composer.gotoScene("my correct scene")
  else composer.gotoScene("my incorrect scene") end
end


local button1 = display.newText( sceneGroup, "correct", 200, 50, native.systemFont, 40 )
button1:translate(200, 700)
button1:addEventListener("tap", onTap)
button1.isCorrect = true

local button2 = display.newText( sceneGroup, "incorrect", 200, 50, native.systemFont, 40 )
button2:translate(500, 700)
button2:addEventListener("tap", onTap)



  • XeduR @Spyric and GO4IT like this

[TOPIC: post.html]
#3

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,757 posts
  • Corona SDK

I second that, while making things modular, reuseable and flexible is the ultimate aim for a programmer, when starting out just get things working.

You can always refactor later on when you better understand how things work and where it is actually worthwhile.
  • GO4IT likes this

[TOPIC: post.html]
#4

GO4IT

[GLOBAL: userInfoPane.html]
GO4IT
  • Observer

  • 13 posts
  • Corona SDK

Indeed. it is good to keep it simple and modular. Sometimes I complicated myself and lost. Thank you!

 

It would be a good idea if there are collections of simple and typical functions/techniques for Lua on the web, which can be nicely listed and easily searchable. I either see specific tutorials, or general tutorials/guides like tutorialpoint, edition, and Lua users. They are good for their purposes, but I often need somewhere in between. If anyone knows such websites, highly appreciated!




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: quiz, clickable link, gotoscene