how to reshuffle array table when button is pressed in the application
To be clear on the topic here is the scenario
when the user got the right or wrong answer of the quiz and went back to the main menu and take again the quiz why does it keeps repeating the same scene over and over again more likely 20 times then suddenly it turns black. . i indicated the code to display the scene index to the terminal to keep track of the process but still the same logical error
You will need to post your code, nobody will be able to guess where the error is coming from without some more information.
local questions1 = {“quiz1”, “quiz2”, “quiz3”};
for index = 1, 0 do
question1[index] = index
end
local shuffle = function(t)
local iteration = #t
local j
for index = iteration, 2, -1 do
j = math.random(index);
t[index], t[j] = t[j], t[index]
return t
end
end
shuffle(questions1)
print (questions1[1])
Well I can’t be sure if this is the cause of your problem, but I can immediately see some mistakes in your for loops:
for index = 1, 0 do question1[index] = index end
Here you have a loop which starts at 1, and counts to 0. However since you haven’t specified -1 as the increment, your loop is trying to count up to 0 from 1, which means nothing happens. If this is a typo then fine, but otherwise you should try:
for index = 1, 0, -1 do question1[index] = index end
Your loop inside the shuffle function has another problem. You are returning the table from inside the loop, rather than waiting until the for loop has finished.
local shuffle = function(t) local iteration = #t local j for index = iteration, 2, -1 do j = math.random(index); t[index], t[j] = t[j], t[index] return t end end
The return t will return the table on the very first pass that this loop makes, which will prevent the rest of the loop from running (I believe). Try moving “return t” to the bottom of the function:
local shuffle = function(t) local iteration = #t local j for index = iteration, 2, -1 do j = math.random(index); t[index], t[j] = t[j], t[index] end return t end
As a general rule, if your function needs to perform an action and then return an object of some kind, the return statement should be at the very end of the function. Obviously there will be times when this is not the case, such as an if/else statement that determines what to return.
If i click the button at the lobby.lua it will go to scene1.lua and display a text then if I press the button it will go back to the lobby then if you repeat the process the diplayed text doesn’t changed at all
lobby.lua
local widget = require("widget") local composer = require("composer") local scene = composer.newScene(); function scene:create (event) local sceneGroup = self.view local sceneChanged = function (event) composer.gotoScene("scene1", "fade", 100); end local button1 = widget.newButton { defaultFile = "Icon.png", onPress = sceneChanged } button1.x=100 button1.y=100 sceneGroup:insert(button1) end function scene:destroy (event) end scene:addEventListener("create", scene) scene:addEventListener("destroy", scene) return scene
i am doing an if/else statement to
scene1.lua
local widget = require (“widget”)
local composer = require (“composer”)
local scene = composer.newScene();
local questions = {“q1”, “q2”, “q3”};
for count=1, 0, -1 do
questions[count] = count
end
local function shuffle(t)
local iterations = #t
local j
for count = iterations,2, -1 do
j = math.random(count)
t[count], t[j] = t[j], t[count]
end
return t
end
shuffle(questions)
function scene:create (event)
local sceneGroup = self.view
if questions[1] == “q1” then
local dispText=display.newText(“hi”,100,200);
sceneGroup:insert(dispText)
end
if questions[1] == “q2” then
local dispText=display.newText(“hello”,100,200);
sceneGroup:insert(dispText)
end
if questions[1] == “q3” then
local dispText=display.newText(“welcome”,100,200);
sceneGroup:insert(dispText)
end
local backScene = function (event)
composer.gotoScene(“lobby”, “fade”, 100)
end
local backButton = widget.newButton
{
defaultFile = “Icon.png”,
onPress = backScene,
}
backButton.x = 100
backButton.y = 100
sceneGroup:insert(backButton)
end
function scene:destroy (event)
end
scene:addEventListener(“create”, scene)
scene:addEventListener(“destroy”, scene)
return scene
Thanks for the help, I just resolved the problem
To be clear on the topic here is the scenario
when the user got the right or wrong answer of the quiz and went back to the main menu and take again the quiz why does it keeps repeating the same scene over and over again more likely 20 times then suddenly it turns black. . i indicated the code to display the scene index to the terminal to keep track of the process but still the same logical error
You will need to post your code, nobody will be able to guess where the error is coming from without some more information.
local questions1 = {“quiz1”, “quiz2”, “quiz3”};
for index = 1, 0 do
question1[index] = index
end
local shuffle = function(t)
local iteration = #t
local j
for index = iteration, 2, -1 do
j = math.random(index);
t[index], t[j] = t[j], t[index]
return t
end
end
shuffle(questions1)
print (questions1[1])
Well I can’t be sure if this is the cause of your problem, but I can immediately see some mistakes in your for loops:
for index = 1, 0 do question1[index] = index end
Here you have a loop which starts at 1, and counts to 0. However since you haven’t specified -1 as the increment, your loop is trying to count up to 0 from 1, which means nothing happens. If this is a typo then fine, but otherwise you should try:
for index = 1, 0, -1 do question1[index] = index end
Your loop inside the shuffle function has another problem. You are returning the table from inside the loop, rather than waiting until the for loop has finished.
local shuffle = function(t) local iteration = #t local j for index = iteration, 2, -1 do j = math.random(index); t[index], t[j] = t[j], t[index] return t end end
The return t will return the table on the very first pass that this loop makes, which will prevent the rest of the loop from running (I believe). Try moving “return t” to the bottom of the function:
local shuffle = function(t) local iteration = #t local j for index = iteration, 2, -1 do j = math.random(index); t[index], t[j] = t[j], t[index] end return t end
As a general rule, if your function needs to perform an action and then return an object of some kind, the return statement should be at the very end of the function. Obviously there will be times when this is not the case, such as an if/else statement that determines what to return.
If i click the button at the lobby.lua it will go to scene1.lua and display a text then if I press the button it will go back to the lobby then if you repeat the process the diplayed text doesn’t changed at all
lobby.lua
local widget = require("widget") local composer = require("composer") local scene = composer.newScene(); function scene:create (event) local sceneGroup = self.view local sceneChanged = function (event) composer.gotoScene("scene1", "fade", 100); end local button1 = widget.newButton { defaultFile = "Icon.png", onPress = sceneChanged } button1.x=100 button1.y=100 sceneGroup:insert(button1) end function scene:destroy (event) end scene:addEventListener("create", scene) scene:addEventListener("destroy", scene) return scene
i am doing an if/else statement to
scene1.lua
local widget = require (“widget”)
local composer = require (“composer”)
local scene = composer.newScene();
local questions = {“q1”, “q2”, “q3”};
for count=1, 0, -1 do
questions[count] = count
end
local function shuffle(t)
local iterations = #t
local j
for count = iterations,2, -1 do
j = math.random(count)
t[count], t[j] = t[j], t[count]
end
return t
end
shuffle(questions)
function scene:create (event)
local sceneGroup = self.view
if questions[1] == “q1” then
local dispText=display.newText(“hi”,100,200);
sceneGroup:insert(dispText)
end
if questions[1] == “q2” then
local dispText=display.newText(“hello”,100,200);
sceneGroup:insert(dispText)
end
if questions[1] == “q3” then
local dispText=display.newText(“welcome”,100,200);
sceneGroup:insert(dispText)
end
local backScene = function (event)
composer.gotoScene(“lobby”, “fade”, 100)
end
local backButton = widget.newButton
{
defaultFile = “Icon.png”,
onPress = backScene,
}
backButton.x = 100
backButton.y = 100
sceneGroup:insert(backButton)
end
function scene:destroy (event)
end
scene:addEventListener(“create”, scene)
scene:addEventListener(“destroy”, scene)
return scene
Thanks for the help, I just resolved the problem