[觸動精靈]零基礎小白學觸動5-8


零基礎小白學觸動 - 05 - 觸動常用函數


點擊 滑動 原理

其實都可以分解成 按下=》 等待一定時間或者移動動作=》  松開 

點擊: tSLib庫的函數tap(x,y) 后面還有2個參數 可以自己看手冊  https://www.zybuluo.com/miniknife/note/293935#函數tap-點擊

滑動  moveTo(x1,y1,x2,y2,step)   詳細的   https://www.zybuluo.com/miniknife/note/293935#函數moveto-滑動

?如何實現精確滑動  https://zimaoxy.com/b/t-860-1-3.html 深入研究  暫時還沒理解思路 而觸動手冊里面給的例子測試過  無法做到完美的精確滑動 就不用了 還有其他模式的滑動 在當前滑動無效的情況下

延時 mSleep()

坐標初始化函數  init(0)  沒什么說的 0是home在下 1是home在右 2是home在左  腳本開始要坐標初始化下  而且不能把init() 放到其他文件然后require導入 是對main.lua無效的 血淚的教訓


小知識:require 調用文件的使用注意
  1. require會自動判斷當前原碼是否已經載入該文件 如果已經載入這個文件就不會再繼續載入 給我們一個省事的用法

  2. 無法疊加require 比如說 我在主腳本里面調用自己的模版 但是在自己的模版里面調用 TSLib庫 這樣是無法在主腳本里面直接調用TSLib庫的 只能直接在主腳本里面載入TSLib

  3. 多個調用文件之間的函數 表 變量的互通情況的注意事項

並不清楚lua實現的原理 但是也勉強總結了一些經驗

1.幾個文件相互調用的情形下  文件內的變量函數表或者其他數據類型 如果想要其他文件也可以自由調用 那么千萬不能加local變成局部變量 個人原來很喜歡盡可能的減少全局變量的數母來提高效率但是這樣會導致很多調用問題 就算是在main.lua的最外層 用local聲明的偽局部變量也不行

2.如果某個被調用文件內的函數或者表 調用了其他文件的變量 函數 或者表  注意調用時候雙方的前后位置  因為 如果 其他文件的變量 函數 或者表 尚未載入完成 我們就引用了 被調用文件內的函數或者表  那么自然參與運算的變量函數表也是空的


小知識:取色器的一個補充 R快捷鍵的作用

用於兩張截圖的多個相同坐標的顏色的對比  比按鍵的切換圖片對比更加精細

image

在第一張圖片上取多個點 這里就在藍色對勾上取點

image

切換到第二個圖片 點擊快捷鍵R 坐標沒變化 但是這里顯示是第二張圖片的顏色了

image

注意:如果按下的每個點后面的按鈕  那么切換圖片后 點擊快捷鍵R 后呈獻下圖

image




小知識:nLog()函數的注意事項

1.輸出日志到觸動精靈 IDE 編輯器 

2.由觸動精靈腳本編輯器發起的腳本運行將會接收到 nLog 回傳信息,其他方式運行的腳本將不會觸發 nLog 函數  的確相當於按鍵的traceprint

for i=1,20 do
     nLog(tostring(i))
end

結果

20
19
18
17
16
15
13
12
11
10
9
6
8
7
5
3
2
1
14

從結果看 不單單輸出的內容是倒着的 而且比如14還出現在1之前 順序也是亂的  是日志是異步生成的 導致的

解決辦法:

nLog之間加上一點點延遲經過測試 延遲為1ms的話依然無法解決問題 可以設置為10ms 就順序正常了

for i=1,20 do
     nLog(tostring(i))
     mSleep(5)
end

為了徹底解決這個問題  暫時想到的解決辦法

function nLogEx(str)
    nLog(str)
    mSleep(10)
end
--下面是最常用的顯示當前哪個文件和第幾行 也對里面nLog做了處理
function traceprint(str)
    local info = debug.getinfo(2) 
    nLog("[" .. tostring(info["source"]) .. "]" .. "" .. tostring(info["currentline"]) .. "" .. tostring(str))
    --nLog("[" .. string.format("%s:%d:", info["source"]:match(".*\\([^\\]*)")) .. "]" .. "第" .. tostring(info["currentline"]) .. "行  " .. tostring(str))    
    mSleep(5)--為了nLog的顯示順序的暫時解決辦法
end



小知識:nLog()函數無法滿足需求  因為我剛發現觸動的日志系統真的很強大 上面那個日志函數並不完善

http://www.cncrk.com/downinfo/117279.html --官方群管理員說這個沒問題  一個局域網查看觸動日志的工具

--【腳本實例】
--1、寫到本地日志
initLog("test", 0);--把 0 換成 1 即生成形似 test_1397679553.log 的日志文件 
wLog("test","[DATE] Test_1 OK!!!"); 
mSleep(500);
wLog("test","[DATE] Test_2 OK!!!");
closeLog("test"); --關閉日志
--2、發送服務器日志
initLog("192.168.1.1", 2); --初始化日志,並以異步方式發送;把 2 換成 3 即為同步發送
wLog("192.168.1.1", "[DATE] Test OK!!!"); --將日志發送到 192.168.1.1
closeLog("192.168.1.1"); --關閉服務器連接
--3、多日志記錄
initLog("test_1", 0);
initLog("test_2", 0); 
wLog("test_1","[DATE] Test_1 OK!!!"); 
mSleep(500);
wLog("test_2","[DATE] Test_2 OK!!!"); 
closeLog("test_1"); closeLog("test_2");


簡易的例子

--避免日志順序不對的對日志函數的封裝
function nLogEx(str)
    nLog(str)
    mSleep(5)
end
--wLog加強版 只需要寫日志內容就好 不需要填寫日志文件名和[data]了
--需要配置表的日志文件元素
--運行后 在日志輸出窗口可以看到Nlog的信息 也可以在對應的日志文件里面找到wLog的日志記錄 
function wLogEx(contents)    
    if config["isLog"] then
        wLog(config["logFileName"], tostring(contents))
        nLogEx(contents)
    end
end

--配置區
config={}
----腳本信息
config["author"]="點滴積累"--作者
config["QQ"]="1847154827"--QQ
config["scriptName"]="測試腳本一"--腳本名字
----日志相關(暫時不考慮多日志並存的情況)
config["isLog"]=true--是否開啟日志(包含日志輸出窗口和日志文件兩部分)
config["logFileName"]=tostring(config["scriptName"]) .. tostring(os.date("%Y%m%d%H%M%S",os.time())) --只是當前日志文件名字不是完整路徑年月日時分秒 如XXXX20190816112336 加了個前綴
config["logMode"]=0--0表示寫入本地日志文件--1 - 生成 1 個以時間戳命名的后綴為 .log 的文件到 log 文件夾下 2 - 異步發送到服務器(支持引擎 v1.7.0 及 Android v2.4.1 以上版本)3 - 同步發送到服務器(僅支持觸動精靈 iOS v1.7.0 及以上版本)

--初始化區
init(0)--設置坐標初始化 這個初始化必須在main.lua下聲明 不然整個坐標都會出問題
if config["isLog"] then
    initLog(tostring(config["logFileName"]), config["logMode"])--設置日志初始化 日志文件名和日志模式 日志名由配置表的元素提供
end



--測試區
for i=1,20 do
    wLogEx("hello" .. tostring(i))
end



--銷毀區--腳本停止前要做的事情
if config["isLog"] then
    closeLog(config["logFileName"]);
end



小知識:beforeUserExit 銷毀事件的觸發

此函數可以被 lua_exit()音量鍵停止遠程接口停止 觸發 但是腳本自己報錯崩潰這個情況他不會觸發 還有腳本正常運行完畢也不會觸發

實際測試哪些能觸發

1.os.exit() X 不會正常觸發beforeUserExit 銷毀事件  不但不會正常的停止 腳本還有一些奇怪的表現 不要用os.exit()停止腳本

2.lua_exit() 以后使用這個停止腳本

3.編輯器上的停止按鈕

4 音量減號



零基礎小白學觸動 - 06 - 如何寫簡單的點擊腳本

這節課只是取坐標然后點擊而已


零基礎小白學觸動 - 07 - 找色

小知識:toast的顯示問題

因為這個東西是異步顯示 下圖是同時呈現toast和nlog

結論:很遺憾 這個toast無法保證實時的顯示信息 和按鍵的showmessage無法比


小知識:老師提到 使用中文變量 可能會影響效率 我自己做了個測試 代碼如下

結果:

結論:中文變量和英文變量基本沒區別 可以安心的使用中文變量  不過為了省事變量還是盡量英文或者拼音把


知識點:觸動的多點找色 多點比色 找圖 循環多點找色 循環多點比色 循環找圖  等待多點找色消失 等點多點比色消失 等待找圖消失

--輸出日志信息到日志窗口和日志文件 日志文件的輸出由配置表控制
function traceprint(str) local info = debug.getinfo(2) local tempStr="[" .. tostring(info["source"]) .. "]" .. "" .. tostring(info["currentline"]) .. "" .. tostring(str) if config["isLog"] then wLog(config["logFileName"], tostring(tempStr)) end nLog(tempStr) mSleep(5)--為了nLog的顯示順序的暫時解決辦法
end
function try(block) local tablejoin = function (...) local result = {} for _, t in ipairs({...}) do
            if type(t) == "table" then
                for k, v in pairs(t) do
                    if type(k) == "number" then table.insert(result, v) else result[k] = v end
                end
            else
                table.insert(result, t) end
        end
        return result end

    -- get the try function
    local try = block[1] assert(try) -- get catch and finally functions
    local funcs = tablejoin(block[2] or {}, block[3] or {}) -- try to call it
    local result_error = {} local results = {pcall(try)} if not results[1] then
        -- run the catch function
        if funcs and funcs.catch then result_error = {funcs.catch(results[2])} end
    end

    -- run the finally function
    if funcs and funcs.finally then
        local result_fin = {funcs.finally(table.unpack(results))} if #result_fin > 0 then
            return table.unpack(result_fin) end
    end

    -- ok?
    if results[1] and #results > 1 then
        return table.unpack(results, 2, #results) else
        if #result_error > 0 then
            return table.unpack(result_error) else
            return nil
        end
    end
end
function catch(block) -- get the catch block function
    return {catch = block[1]} end
function finally(block) -- get the finally block function
    return {finally = block[1]} end
--[[ 功能:多點找色 interface 界面 window 窗口 Button 按鈕 參數:就是一個表 基本結構 ddzs_zhujiemian_kaishiyouxiButton={"游戲主界面_開始游戲按鈕",color, posandcolor, degree, x1, y1, x2, y2,{orient=1, main = 0x101010,list = 0x202020}} 返回值:-1 1 -1表示找不到 1表示找到 坐標在全局變量intX,intY里面 --]]
function ddzs(tempTable) -- 1.檢測傳遞進來的參數的合法性 這里就罷了 
    -- 2.開始傳遞進來參數進行多點找色 
    -- 3.判斷查找結果 返回函數返回值
    return try{ function () --下面代碼隨便寫 有可能拋出異常即可
            local result=-1
            local startTime=os.time() local mark=tempTable[1] local color=tempTable[2] local posandcolor=tempTable[3] local degree=tempTable[4] local x1=tempTable[5]  local y1=tempTable[6]  local x2=tempTable[7] local y2=tempTable[8] local tempX=-1 local tempY=-1

            if type(tempTable[9])=="table" then tempX, tempY = findMultiColorInRegionFuzzy(color, posandcolor, degree, x1, y1, x2, y2,tempTable[9]) else tempX, tempY = findMultiColorInRegionFuzzy(color, posandcolor, degree, x1, y1, x2, y2) end

            if tempX>-1  and tempY>-1  then result=1 traceprint("[多點找色] [" .. tostring(mark) .. "] 坐標:" .. tostring(tempX) .. "," ..tostring(tempY)) intX=tempX  intY=tempY else traceprint("[多點找色] <" .. tostring(mark) .. "> 沒找到 ") end

            return result end, catch{ function (errors) --這里對應函數名要改
                traceprint("函數[" .. tostring("ddzs") .. "] <" .. tostring(tempTable[1] ) .. "> 錯誤信息:".. tostring(errors)) end } } end

--[[ 多點比色 參數 {"游戲主界面_開始游戲按鈕",array,dim,flag} 注意顏色數組是x1,y1,顏色數值 而不是以|分隔 返回值:-1 1 -1表示找不到 1表示找到 坐標在全局變量intX,intY里面 坐標其實就是顏色組第一個元素的坐標 --]]
function ddbs(tempTable) return try{ function () --下面代碼隨便寫 有可能拋出異常即可
            local result=-1
            local startTime=os.time() local mark=tempTable[1] local tempArray=tempTable[2] local dim=tempTable[3] local flag=tempTable[4] local tempX=-1 local tempY=-1

            if multiColor(tempArray,dim,flag) == true then tempX=tonumber( tempArray[1][1]) or -1 tempY=tonumber( tempArray[1][2]) or -1
                if tempX==-1 or  tempY==-1 then
                    error("第一顏色項坐標存在問題") else result=1 traceprint("[多點比色] [" .. tostring(mark) .. "] 坐標:" .. tostring(tempX) .. "," ..tostring(tempY)) intX=tempX  intY=tempY end
            else traceprint("[多點比色] <" .. tostring(mark) .. "> 沒找到 ") end
            return result end, catch{ function (errors) --這里對應函數名要改
                traceprint("函數[" .. tostring("ddbs") .. "] <" .. tostring(tempTable[1] ) .. "> 錯誤信息:".. tostring(errors)) end } } end

--[[ --找圖 不過觸動對找圖支持的好差 截圖還需要用ps 參數 {"游戲主界面_開始游戲按鈕",picpath, degree, x1, y1, x2, y2, alpha, type} 返回值:-1 1 -1表示找不到 1表示找到 坐標在全局變量intX,intY里面 --]]
function zt(tempTable) return try{ function () --下面代碼隨便寫 有可能拋出異常即可
            local result=-1
            local startTime=os.time() local mark=tempTable[1] local picpath=tempTable[2] local degree=tempTable[3] local x1=tempTable[4]  local y1=tempTable[5]  local x2=tempTable[6] local y2=tempTable[7] local alpha=tempTable[8] or 0
            local tempType=tempTable[9] local tempX=-1 local tempY=-1

            if tempType==nil then tempX, tempY = findImageInRegionFuzzy(picpath, degree, x1, y1, x2, y2, alpha) else tempX, tempY = findImageInRegionFuzzy(picpath, degree, x1, y1, x2, y2, alpha,tempType) end



            if tempX>-1  and tempY>-1  then result=1 traceprint("[找圖] [" .. tostring(mark) .. "] 坐標:" .. tostring(tempX) .. "," ..tostring(tempY)) intX=tempX  intY=tempY else traceprint("[找圖] <" .. tostring(mark) .. "> 沒找到 ") end
            return result end, catch{ function (errors) --這里對應函數名要改
                traceprint("函數[" .. tostring("zt") .. "] <" .. tostring(tempTable[1] ) .. "> 錯誤信息:".. tostring(errors)) end } } end

--[[ 循環多點找色 參數 ({"游戲主界面_開始游戲按鈕",color, posandcolor, degree, x1, y1, x2, y2,{orient=1, main = 0x101010,list = 0x202020}},10) 返回值:-1 1 -1表示找不到 1表示找到 坐標在全局變量intX,intY里面 --]]
function waitDdzs(tempTable,limitTime) return try{ function () --下面代碼隨便寫 有可能拋出異常即可
            local result=-1
            local startTime=os.time() local mark=tempTable[1] local color=tempTable[2] local posandcolor=tempTable[3] local degree=tempTable[4] local x1=tempTable[5]  local y1=tempTable[6]  local x2=tempTable[7] local y2=tempTable[8] local tempX=-1 local tempY=-1 limitTime=tonumber(limitTime) or 5--默認是5秒限制
            while(true) do
               if (os.time()- startTime)>limitTime then
                    --traceprint("到時間了跳出")
                    result=-1
                    break
               end
                if type(tempTable[9])=="table" then tempX, tempY = findMultiColorInRegionFuzzy(color, posandcolor, degree, x1, y1, x2, y2,tempTable[9]) else tempX, tempY = findMultiColorInRegionFuzzy(color, posandcolor, degree, x1, y1, x2, y2) end

                if tempX>-1  and tempY>-1  then result=1 intX=tempX  intY=tempY --traceprint("找到了跳出")
                    break
                else
                            
                end mSleep(100) end        
            if result==1  then traceprint("[循環多點找色] [" .. tostring(mark) .. "] 坐標:" .. tostring(tempX) .. "," ..tostring(tempY)) else traceprint("[循環多點找色] <" .. tostring(mark) .. "> 沒找到 ") end
    
            return result end, catch{ function (errors) --這里對應函數名要改
                traceprint("函數[" .. tostring("waitDdzs") .. "] <" .. tostring(tempTable[1] ) .. "> 錯誤信息:".. tostring(errors)) end } } end
--循環多點比色 
function waitDdbs(tempTable,limitTime) return try{ function () --下面代碼隨便寫 有可能拋出異常即可
            local result=-1
            local startTime=os.time() local mark=tempTable[1] local tempArray=tempTable[2] local dim=tempTable[3] local flag=tempTable[4] local tempX=-1 local tempY=-1 limitTime=tonumber(limitTime) or 5--默認是5秒限制
            while(true) do
               if (os.time()- startTime)>limitTime then
                    --traceprint("到時間了跳出")
                    result=-1
                    break
               end
                if multiColor(tempArray,dim,flag) == true then tempX=tonumber( tempArray[1][1]) or -1 tempY=tonumber( tempArray[1][2]) or -1
                    if tempX==-1 or  tempY==-1 then
                        error("第一顏色項坐標存在問題") else result=1 intX=tempX  intY=tempY break
                    end
                else
                    
                end mSleep(100) end
            if result==1  then traceprint("[循環多點比色] [" .. tostring(mark) .. "] 坐標:" .. tostring(tempX) .. "," ..tostring(tempY)) else traceprint("[循環多點比色] <" .. tostring(mark) .. "> 沒找到 ") end
            return result end, catch{ function (errors) --這里對應函數名要改
                traceprint("函數[" .. tostring("waitDdbs") .. "] <" .. tostring(tempTable[1] ) .. "> 錯誤信息:".. tostring(errors)) end } } end
--循環找圖
function waitZt(tempTable,limitTime) return try{ function () --下面代碼隨便寫 有可能拋出異常即可
            local result=-1
            local startTime=os.time() local mark=tempTable[1] local picpath=tempTable[2] local degree=tempTable[3] local x1=tempTable[4]  local y1=tempTable[5]  local x2=tempTable[6] local y2=tempTable[7] local alpha=tempTable[8] or 0
            local tempType=tempTable[9] local tempX=-1 local tempY=-1 limitTime=tonumber(limitTime) or 5--默認是5秒限制
            
            while(true) do
               if (os.time()- startTime)>limitTime then
                    --traceprint("到時間了跳出")
                    result=-1
                    break
               end
                if tempType==nil then tempX, tempY = findImageInRegionFuzzy(picpath, degree, x1, y1, x2, y2, alpha) else tempX, tempY = findImageInRegionFuzzy(picpath, degree, x1, y1, x2, y2, alpha,tempType) end
                if tempX>-1  and tempY>-1  then result=1 intX=tempX  intY=tempY break
                else
                        
                end mSleep(100) end   
            
            
            if result==1  then traceprint("[循環找圖] [" .. tostring(mark) .. "] 坐標:" .. tostring(tempX) .. "," ..tostring(tempY)) else traceprint("[循環找圖] <" .. tostring(mark) .. "> 沒找到 ") end
            return result end, catch{ function (errors) --這里對應函數名要改
                traceprint("函數[" .. tostring("waitZt") .. "] <" .. tostring(tempTable[1] ) .. "> 錯誤信息:".. tostring(errors)) end } } end
--等待多點找色消失
function waitDdzsDisappear(tempTable,limitTime) return try{ function () --下面代碼隨便寫 有可能拋出異常即可
            local result=-1
            local startTime=os.time() local mark=tempTable[1] local color=tempTable[2] local posandcolor=tempTable[3] local degree=tempTable[4] local x1=tempTable[5]  local y1=tempTable[6]  local x2=tempTable[7] local y2=tempTable[8] local tempX=-1 local tempY=-1 limitTime=tonumber(limitTime) or 5--默認是5秒限制
            while(true) do
               if (os.time()- startTime)>limitTime then
                    break
               end
                if type(tempTable[9])=="table" then tempX, tempY = findMultiColorInRegionFuzzy(color, posandcolor, degree, x1, y1, x2, y2,tempTable[9]) else tempX, tempY = findMultiColorInRegionFuzzy(color, posandcolor, degree, x1, y1, x2, y2) end

                if tempX>-1  and tempY>-1  then
                else
                    break--找不到了才跳出
                end mSleep(100) end        
            if result==1  then traceprint("[等待多點找色消失] <" .. tostring(mark) .. "> 消失失敗") else traceprint("[等待多點找色消失] [" .. tostring(mark) .. "] 消失成功 ") end
    
            --return result
        end, catch{ function (errors) --這里對應函數名要改
                traceprint("函數[" .. tostring("waitDdzsDisappear") .. "] <" .. tostring(tempTable[1] ) .. "> 錯誤信息:".. tostring(errors)) end } } end
--等待多點比色消失
function waitDdbsDisappear(tempTable,limitTime) return try{ function () --下面代碼隨便寫 有可能拋出異常即可
            local result=-1
            local startTime=os.time() local mark=tempTable[1] local tempArray=tempTable[2] local dim=tempTable[3] local flag=tempTable[4] local tempX=-1 local tempY=-1 limitTime=tonumber(limitTime) or 5--默認是5秒限制
            while(true) do
               if (os.time()- startTime)>limitTime then
                    break
               end
                if multiColor(tempArray,dim,flag) == true then tempX=tonumber( tempArray[1][1]) or -1 tempY=tonumber( tempArray[1][2]) or -1
                    if tempX==-1 or  tempY==-1 then
                        error("第一顏色項坐標存在問題") else result=1
                    end
                else result=-1
                    break
                end mSleep(100) end
            if result==1  then traceprint("[等待多點比色消失] <" .. tostring(mark) .. "> 消失失敗") else traceprint("[等待多點比色消失] [" .. tostring(mark) .. "] 消失成功 ") end
            --return result
        end, catch{ function (errors) --這里對應函數名要改
                traceprint("函數[" .. tostring("waitDdbsDisappear") .. "] <" .. tostring(tempTable[1] ) .. "> 錯誤信息:".. tostring(errors)) end } } end
--等待找圖消失
function waitZtDisappear(tempTable,limitTime) return try{ function () --下面代碼隨便寫 有可能拋出異常即可
            local result=-1
            local startTime=os.time() local mark=tempTable[1] local picpath=tempTable[2] local degree=tempTable[3] local x1=tempTable[4]  local y1=tempTable[5]  local x2=tempTable[6] local y2=tempTable[7] local alpha=tempTable[8] or 0
            local tempType=tempTable[9] local tempX=-1 local tempY=-1 limitTime=tonumber(limitTime) or 5--默認是5秒限制
            
            while(true) do
               if (os.time()- startTime)>limitTime then result=-1
                    break
               end
                if tempType==nil then tempX, tempY = findImageInRegionFuzzy(picpath, degree, x1, y1, x2, y2, alpha) else tempX, tempY = findImageInRegionFuzzy(picpath, degree, x1, y1, x2, y2, alpha,tempType) end
                if tempX>-1  and tempY>-1  then result=1
                else result=-1
                    break    
                end mSleep(100) end   
            
            
            if result==1  then traceprint("[等待找圖消失] <" .. tostring(mark) .. "> 消失失敗") else traceprint("[等待找圖消失] [" .. tostring(mark) .. "] 消失成功 ") end
            --return result
        end, catch{ function (errors) --這里對應函數名要改
                traceprint("函數[" .. tostring("waitZtDisappear") .. "] <" .. tostring(tempTable[1] ) .. "> 錯誤信息:".. tostring(errors)) end } } end




零基礎小白學觸動 - 08 - 如何寫更智能的找色點擊腳本

小知識:可以直接連接手機ip+端口 直接在手機上寫代碼

但是不推薦這個方式 不成熟 沒有意義



本節課沒什么 老師演示了下 一個用多點找色 實現找箱子的實例


知識點:關於lua下的錯誤處理機制(感謝紫貓插件的源碼提供的錯誤處理函數) 這4個函數構建了整個錯誤處理機制 讓腳本函數出錯不至於腳本崩潰 並且可以日志報錯  之后我們所有函數都要類似下面的test11的結構 閉包形式 和try  catch 形式

function traceprint(str)
    local info = debug.getinfo(2) 
    nLog("[" .. tostring(info["source"]) .. "]" .. "" .. tostring(info["currentline"]) .. "" .. tostring(str))
    --nLog("[" .. string.format("%s:%d:", info["source"]:match(".*\\([^\\]*)")) .. "]" .. "第" .. tostring(info["currentline"]) .. "行  " .. tostring(str))    
    mSleep(5)--為了nLog的顯示順序的暫時解決辦法
end
function try(block)
    local tablejoin = function (...)
        local result = {}
        for _, t in ipairs({...}) do
            if type(t) == "table" then
                for k, v in pairs(t) do
                    if type(k) == "number" then table.insert(result, v)
                    else result[k] = v end
                end
            else
                table.insert(result, t)
            end
        end
        return result
    end
 
    -- get the try function
    local try = block[1]
    assert(try)
     
    -- get catch and finally functions
    local funcs = tablejoin(block[2] or {}, block[3] or {})
     
    -- try to call it
    local result_error = {}
    local results = {pcall(try)}
    if not results[1] then
        -- run the catch function
        if funcs and funcs.catch then
            result_error = {funcs.catch(results[2])}
        end
    end
 
    -- run the finally function
    if funcs and funcs.finally then
        local result_fin = {funcs.finally(table.unpack(results))}
        if #result_fin > 0 then
            return table.unpack(result_fin)
        end
    end
 
    -- ok?
    if results[1] and #results > 1 then
        return table.unpack(results, 2, #results)
    else
        if #result_error > 0 then
            return table.unpack(result_error)
        else
            return nil
        end
    end
end
function catch(block)
    -- get the catch block function
    return {catch = block[1]}
end
function finally(block)
    -- get the finally block function
    return {finally = block[1]}
end
--出錯函數 這種結構就是我們的每個函數的基本結構 因為這樣的結構可以保證就算函數出錯了 腳本也不會崩潰同時這也是閉包結構
function test11(a)
    return    try{
            function ()
                if a==1 then
                    error("error a==1")--拋出異常
                else
                    --error("error a~=1")
                    return a
                end                
            end,--注意這有逗號
            --捕獲異常
            catch{
                function (errors)
                    traceprint("test11 發生運行時錯誤!錯誤信息:".. tostring(errors))
                end
            }
        }

end


--這里必然出錯 但是腳本沒有崩潰 被錯誤函數處理完畢了 后面的代碼依然正常執行
traceprint(test11(1))--[main.lua]第45行  test11 發生運行時錯誤!錯誤信息:[string "main.lua"]:35: error a==1
traceprint("1111111111")--[main.lua]第57行  1111111111


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM