(Corona SDK)如何取消math.random?

时间:2012-08-01 08:23:37

标签: math random sdk corona

我在这里有一些代码:

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? 提前谢谢!

1 个答案:

答案 0 :(得分:0)

我不清楚你在这里问的是什么,但好像你有几个问题。

  1. 您已经丢失了作为框架侦听器添加的主要功能的句柄
  2. 每次检测到time_remaining时,都会重复添加&gt; 1,意味着它每帧运行多次
  3. 您正在使用计时器句柄替换对menuLoad的引用(尽管这可能是故意的。
  4. 我在代码中推断你希望粒子在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)