0

这里我有一些代码:

function CountDown()
             if(time_remaining > 1)then
                 time_remaining = time_remaining - 1;
                 print ("Loading menu")  
                  local function main( event )

                    -- LAUNCH A ROCKET 
                    if math.ceil(math.random() * 200) == 10 and launch == false then
                        Particles.GetEmitter  ("Launcher1").rotation = -35
                        Particles.GetEmitter  ("Launcher2").rotation = 20
                        Particles.StartEmitter("Launcher1", true)
                        Particles.StartEmitter("Launcher2", true)

                    end

                    -- UPDATE PARTICLES
                    Particles.Update()

                end

                -- timer.performWithDelay( 33, main, 1 )
                Runtime:addEventListener( "enterFrame", main )
             else
                 time_remaining = 0
                 print ("Load Go!")
                 menuLoad = transition.to( menuLoad, { time=575, y=-500 })
             end

        end

        count_timer = timer.performWithDelay(1000, CountDown, time_total);

当我切换场景时,我通过 Particles.CleanUp() 取消了所有发射器,但我无法取消 math.random,无论如何它都会尝试启动我的发射器,但是因为它们已经是 nils (Particles.CleanUp),所以它给了我一个错误

Runtime error
    ...me development/Skipjack Rollout Design2/mainmenu.lua:560: attempt to index a nil value
stack traceback:
    [C]: ?
    ...me development/Skipjack Rollout Design2/mainmenu.lua:560: in function <...me development/Skipjack Rollout Design2/mainmenu.lua:556>
    ?: in function <?:226>

请帮帮我!我如何取消 math.random?提前致谢!

4

1 回答 1

0

我不完全清楚你在这里问什么,但似乎你有几个问题。

  1. 您丢失了作为帧侦听器添加的主函数的句柄
  2. 每次检测到 time_remaining > 1 时都会重复添加它,这意味着它每帧运行多次
  3. 您正在用计时器句柄替换对 menuLoad 的引用(尽管这可能是故意的。

我从代码中推断您希望您的粒子播放 time_total 秒,此时您将转换到 menuLoad?如果是这样,那么以下情况如何:

local function main(event)
    -- LAUNCH A ROCKET
    if math.ceil(math.random() * 200) == 10 and launch == false then
        Particles.GetEmitter  ("Launcher1").rotation = -35
        Particles.GetEmitter  ("Launcher2").rotation = 20
        Particles.StartEmitter("Launcher1", true)
        Particles.StartEmitter("Launcher2", true)
    end

    -- UPDATE PARTICLES
    Particles.Update()
end

Runtime:addEventListener("enterFrame", main)
timer.performWithDelay(time_total * 1000, function() 
    Runtime:removeEventListener("enterFrame", main)
    transition.to( menuLoad, { time=575, y=-500 })
end)
于 2012-08-09T19:34:41.470 回答