【问题标题】:(Corona SDK) How cancel math.random?(Corona SDK) 如何取消 math.random?
【发布时间】:2012-08-01 08:23:37
【问题描述】:

这里有一些代码:

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

【问题讨论】:

    标签: math random sdk coronasdk


    【解决方案1】:

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

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-20
      • 1970-01-01
      • 2015-02-14
      • 1970-01-01
      • 2013-12-18
      • 2017-03-14
      • 2012-01-28
      • 1970-01-01
      相关资源
      最近更新 更多