初学者具有Lua循环功能

时间:2014-05-23 18:58:45

标签: lua

我是lua的新手,昨晚刚刚开始。我为我的游戏编写了一个脚本,我发现它没有运行。

我的脚本中有三个功能,它们是功能: function shoot() function pickplayer() function drinkwater() < / strong>即可。

  1. function shoot() 不间断运行35秒
  2. 然后在 function shoot() 完成后立即运行功能pickplayer()
  3. 循环步骤1和2 3次
  4. 然后运行 function drinkwater()
  5. 我已经单独运行了3个这样的功能并且工作正常,但是当我将它们组合在一起时,脚本根本不能正常工作。因为在跳转到其他功能之前,它无法在 function shoot() 不间断运行35秒

    如果你们能帮助我指出问题所在,那将是非常值得欣赏的,这样我才能继续前进。

    我知道提出这些愚蠢的问题可能太愚蠢了,但对于一个非计算机的人来说,你的帮助会给我带来巨大的改善

    请参阅下面的脚本

    DESCRIPTION=" first script";
    function shoot()
        while true do
            x, y = findImage("/mnt/sdcard/target1.bmp");
            x1, y1 = findImageFuzzy("/mnt/sdcard/b7.bmp", 80);
            if x1 ~= -1 == x ~= -1 and y1 ~= -1 == y ~= -1 then 
                touchDown(0, x, y);
                touchUp(0)          
            end
            coroutine.yield();      
        end
    end
    
    
    function pickplayer()
        while true do 
            if findImage("/mnt/sdcard/df.bmp") then
                touchDown(0, 355, 783)
                touchUp(0)
                mSleep(500);
                touchDown(0, 188, 203)
                touchUp(0)
                mSleep(500);
                touchDown(0, 196, 196)
                touchUp(0)
                mSleep(500);                        
            end
            mSleep(500);
            coroutine.yield();
        end         
    end
    
    
    function drinkwater()
        while true do
            if findImage("/mnt/sdcard/noenoughwater.bmp") then
                touchDown(0, 228, 479)
                touchUp(0)
                mSleep(2000);
                touchDown(0, 178, 223)
                touchUp(0)
                mSleep(2000);
                touchDown(0, 222, 604)
                touchUp(0)
                mSleep(2000);
                touchDown(0, 180, 218)
                touchUp(0)
                mSleep(3000);           
            end
            coroutine.yield();      
        end
    end
    
    
    function main()
        co1 = coroutine.create(shoot);
        co2 = coroutine.create(pickplayer);  
        co3 = coroutine.create(drinkwater); 
    
        while true do
            local timeToRun = 35000
            local initialTime = os.time()
            local timeElasped=os.difftime(os.time(), initialTime)
            while timeElasped < timeToRun do
                coroutine.resume(co1)
                timeElasped =os.difftime(os.time(), initialTime)
                mSleep(2000);   
                coroutine.resume(co2);
                coroutine.resume(co3);      
            end
        end     
    end
    

1 个答案:

答案 0 :(得分:1)

所有touchDown,ups和sleep都是噪音,这里的问题是&#34; schedule&#34;协程。你基本上有这个:

function condition1() return true end -- for testing
function condition2() return true end -- for testing


function shoot()
    while true do
        -- do stuff, then:
        coroutine.yield()
    end
end


function pickplayer()
    while true do 
        if condition1() then
            -- do stuff, then:
            mSleep(1500)                       
        end
        mSleep(500)
        coroutine.yield()
    end         
end


function drinkwater()
    while true do
        if condition2() then
            -- do stuff, then:
            mSleep(9000)       
        end
        coroutine.yield() 
    end
end


function main()
    co1 = coroutine.create(shoot)
    co2 = coroutine.create(pickplayer)
    co3 = coroutine.create(drinkwater)

    while true do
        local timeToRun = 35000
        local initialTime = os.time()
        local timeElasped=os.difftime(os.time(), initialTime)
        while timeElasped < timeToRun do
            coroutine.resume(co1)
            timeElasped =os.difftime(os.time(), initialTime)
            mSleep(2000)
            coroutine.resume(co2)
            coroutine.resume(co3)  
        end
    end     
end

以上将会这样做:

  1. 运行功能拍摄()
  2. 然后运行函数pickplayer()
  3. 然后运行功能drinkwater()
  4. 永远循环步骤1到3
  5. 但是你说你想实现这个目标:

    1. 运行功能拍摄()不间断35秒
    2. 然后运行函数pickplayer()
    3. 将步骤1和2循环3次
    4. 然后运行函数drinkwater()
    5. 这将要求你的主要做以下事情(不是重复 - 直到这里比在结束时更好):

      function main()
          co1 = coroutine.create(shoot)
          co2 = coroutine.create(pickplayer)
          co3 = coroutine.create(drinkwater)
      
          for i=1,3 do -- do the following 3 times:
              -- shoot for 35 seconds non-stop
              local timeToRun = 35000
              local initialTime = os.time()
              local timeElasped = 0
              repeat 
                  coroutine.resume(co1)
                  timeElasped = os.difftime(os.time(), initialTime)
              until timeElasped < timeToRun 
      
              -- mSleep(2000): don't need this
      
              coroutine.resume(co2) -- pickplayer
          end     
          coroutine.resume(co3)  
      end