【问题标题】:Beginner with Lua looping functionLua循环功能初学者
【发布时间】:2014-05-24 06:06:19
【问题描述】:

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

我的脚本中有三个函数,它们是函数:function shoot()function pickplayer()function drinkwater()

  1. 连续运行 function shoot() 35 秒
  2. 然后在 function shoot() 完成后立即运行 function pickplayer()
  3. 将步骤 1 和 2 循环 3 次
  4. 然后运行 ​​function drinkwater()

我已经单独运行了其中的 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

【问题讨论】:

  • 按原样运行脚本时会发生什么?
  • 感谢您的回复 Etan,未找到错误,只是没有运行
  • 我的计时器有什么问题吗?
  • 刚刚再次测试,函数co1工作正常,但co 2和co3不行
  • “什么都没有运行”是什么意思?究竟会发生什么?如果您将打印/日志语句放在这些函数中,您会看到它们的输出吗?

标签: lua


【解决方案1】:

所有的 touchDown 和 ups 和 sleep 都是噪音,这里的问题是协程的“调度”。你基本上有这个:

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. 运行函数 shoot()
  2. 然后运行函数pickplayer()
  3. 然后运行函数drinkwater()
  4. 永远循环步骤 1 到 3

但是你说你想实现这个:

  1. 连续运行函数 shoot() 35 秒
  2. 然后运行函数pickplayer()
  3. 循环步骤 1 和 2 3 次
  4. 然后运行函数drinkwater()

这将需要您的 main 执行以下操作(并不是这里的 repeat-until 比 while-do-end 更好):

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

【讨论】:

  • 嗨 Schollii,非常感谢您为我编辑此内容。
  • 我是否必须在函数中添加任何内容 condition1() return true end ??
  • 用你的 findImage 条件替换它们,我使用这些是为了专注于结构和日程安排。
  • schollii,有什么我可以亲自和你谈谈的吗?我有很多想问你
  • 你认为你也能为我解决这个问题吗?见下面的链接
猜你喜欢
  • 2018-02-04
  • 1970-01-01
  • 2016-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-16
  • 2012-11-19
  • 2013-03-29
相关资源
最近更新 更多