2014-09-23 57 views
2

。 。 ,使用composer.gotoScene运行随机场景时遇到问题,只有应用程序的第一个场景以随机形式显示。这是一个问答游戏,我不知道如何用分数调用result.lua。使用阵列在电晕sdk中的随机场景

实际测试:

1st attempt = scene2, scene3, scene4, scene1 
2nd attempt = scene3, scene4, scene1, scene2 
3rd attempt = scene4, scene1, scene2, scene3 

预期输出:

1st attempt = scene3, scene1, scene4, scene2, result 
2nd attempt = scene1, scene4, scene2, scene3, result 

这里是我的洗牌代码:

local sceneNames = {"scene1","scene2","scene3","scene4"}; 
for count=1, 0 do 
    sceneNames[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 
end 

shuffle(sceneNames) 

我不知道在哪里设置这还是我需要做。 。 。请帮忙

+1

请参阅http://stackoverflow.com/a/25695927/107090。 – lhf 2014-09-23 18:43:28

+0

[从表中设置表随机值]的可能重复(http://stackoverflow.com/questions/25695807/set-table-random-value-from-table) – hjpotter92 2014-09-24 07:42:22

回答

0

你忘了种子。这应该工作:

local sceneNames = {"scene1","scene2","scene3","scene4"} 

local function shuffle(t) 
    local iterations = #t 
    for count = iterations,2, -1 do 
     local j = math.random(count) 
     t[count], t[j] = t[j], t[count] 
    end 
end 

要使用,运行

math.randomseed(os.time()) 
shuffle(sceneNames) 

多次。每次都做

for i,v in ipairs(sceneNames) do print(i,v) end 

看到序列每次随机变化。