零基礎小白學觸動 - 05 - 觸動經常使用函數 html
點擊 滑動 原理 數組
其實均可以分解成 按下=》 等待必定時間或者移動動做=》 鬆開 服務器
點擊: 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-滑動app
?如何實現精確滑動 https://zimaoxy.com/b/t-860-1-3.html 深刻研究 暫時還沒理解思路 而觸動手冊裏面給的例子測試過 沒法作到完美的精確滑動 就不用了 還有其餘模式的滑動 在當前滑動無效的狀況下異步
延時 mSleep()編輯器
座標初始化函數 init(0) 沒什麼說的 0是home在下 1是home在右 2是home在左 腳本開始要座標初始化下 並且不能把init() 放到其餘文件而後require導入 是對main.lua無效的 血淚的教訓函數
require會自動判斷當前原碼是否已經載入該文件 若是已經載入這個文件就不會再繼續載入 給咱們一個省事的用法 工具
沒法疊加require 好比說 我在主腳本里面調用本身的模版 可是在本身的模版裏面調用 TSLib庫 這樣是沒法在主腳本里面直接調用TSLib庫的 只能直接在主腳本里面載入TSLib 測試
多個調用文件之間的函數 表 變量的互通狀況的注意事項
並不清楚lua實現的原理 可是也勉強總結了一些經驗
1.幾個文件相互調用的情形下 文件內的變量函數表或者其餘數據類型 若是想要其餘文件也能夠自由調用 那麼千萬不能加local變成局部變量 我的原來很喜歡儘量的減小全局變量的數母來提升效率可是這樣會致使不少調用問題 就算是在main.lua的最外層 用local聲明的僞局部變量也不行
2.若是某個被調用文件內的函數或者表 調用了其餘文件的變量 函數 或者表 注意調用時候雙方的先後位置 由於 若是 其餘文件的變量 函數 或者表 還沒有載入完成 咱們就引用了 被調用文件內的函數或者表 那麼天然參與運算的變量函數表也是空的
小知識:取色器的一個補充 R快捷鍵的做用
用於兩張截圖的多個相同座標的顏色的對比 比按鍵的切換圖片對比更加精細
在第一張圖片上取多個點 這裏就在藍色對勾上取點
切換到第二個圖片 點擊快捷鍵R 座標沒變化 可是這裏顯示是第二張圖片的顏色了
注意:若是按下的每一個點後面的按鈕 那麼切換圖片後 點擊快捷鍵R 後呈獻下圖
小知識: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 --官方羣管理員說這個沒問題 一個局域網查看觸動日誌的工具
--【腳本實例】 --一、寫到本地日誌 initLog("test", 0);--把 0 換成 1 即生成形似 test_1397679553.log 的日誌文件 wLog("test","[DATE] Test_1 OK!!!"); mSleep(500); wLog("test","[DATE] Test_2 OK!!!"); closeLog("test"); --關閉日誌 --二、發送服務器日誌 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"); --關閉服務器鏈接 --三、多日誌記錄 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