[触动精灵]零基础小白学触动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快捷键的作用

用于两张截图的多个相同坐标的颜色的对比  比按键的切换图片对比更加精细

在第一张图片上取多个点 这里就在蓝色对勾上取点

切换到第二个图片 点击快捷键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 --官方群管理员说这个没问题  一个局域网查看触动日志的工具

--【脚本实例】
--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


原文地址:https://www.cnblogs.com/zjl8455482/p/11369925.html

时间: 2024-08-11 09:47:02

[触动精灵]零基础小白学触动5-8的相关文章

[触动精灵] 零基础小白学触动1-4

视频地址 http://www.iqiyi.com/playlist443635102.html 零基础小白学触动 - 01 - 说在前面的废话 小知识:Tslib库的定位 是官方为了解决小白不会封装自己的函数 提供了一些常见的封装功能 熟练后 我们可以自己封装自己的函数实现功能 可以不用载入这个库文件 小知识:手册非常强大 手册的搜索功能 和目录列表 ? 零基础小白学触动 - 02 - 注释和循环语句 没什么可说的 注释语句: -- --[[]]-- 流程语句 If 条件1 then Else

[触动精灵] 零基础小白学触动9-12

零基础小白学触动 - 09 - TSLib:点击和比色 没什么说的 tap(x,y,ms) 了解:TSLib库 注意以前库文件针对环境不同 库文件也是不同的  比如TSL ib库 对于基础版  专业版   帮你玩版  是不同的库文件 但是现在似乎整合了一部分 其他库文件也有这样的情况 使用的时候注意先看手册 看看当前环境下使用什么库文件 10 - 弹窗检测和无限点击 基础应用 11 - 如何滑动到界面底部 没什么说的 12 - TSLib 常用函数:便携 UI 细节查看http://www.to

[触动精灵] 零基础小白学触动13-16

小知识:触动精灵载入其他lua文件 require 以前我一直以为require只能加载模块文件 但是其实可以加载普通的其他lua文件  用法还是不加扩展名 触动精灵加载文件 都用require就可以 13 - 自动打开和关闭应用 小知识:触动下开启app和关闭app 查看对应app的包名 官方的runApp函数还可能存在无法启动app的情况 所以我就给封装了一下 万一出现无法启动 包名没有安装等情况 就日志记录 并且有弹窗提示 下面代码  没有traceprint try catch 等函数本

零基础java培训靠谱吗?职场转行,零基础开始学Java开发靠谱吗?

学技术转行发展,是职场常见的提升方式,无论是在职充电还是为转行跳槽做准备,选择一个专业技能进行培训学习,都是非常可取的.在能力至上的今天,单凭学历已经不能成为入行敲门砖,特别是在互联网企业,通常在面试过程中就会考核技术能力,此外看你的项目作品,可见技术能力在招聘中是最具说服力的.华清远见教育职业规划专家表示零基础java培训靠谱吗,如果不清楚学什么技术更好,可以访问这里做职业规划,此外还可以通过试学来了解自己对技术课程的兴趣点. 华清远见教育开设的面向零基础人群提供的,从学习到就业一站式的浸入式

零基础小白,如何入门计算机视觉?

计算机视觉是人工智能技术的一个重要领域,打个比方(不一定恰当),我认为计算机视觉是人工智能时代的眼睛,可见其重要程度.计算机视觉其实是一个很宏大的概念,下图是有人总结的计算机视觉所需要的技能树. 如果你是一个对计算机视觉一无所知的小白,千万不要被这棵技能树吓到.没有哪个人能够同时掌握以上所有的技能,这棵树只是让你对计算机视觉有个粗浅的认识. 先来打点鸡血,看看计算机视觉有什么用吧.下面的视频是计算机视觉在自动驾驶上的实际应用,其中涉及立体视觉.光流估计.视觉里程计.三维物体检测与识别.三维物体跟

零基础小白入门Java免费视频教程推荐—小猿圈

Java语言作为世界上使用最多的开发语言,企业庞大的需求量让每年进入Java开发领域的人不减反增.对于零基础小白来讲如何学习Java开发语言是入门的第一个难题.本文小猿圈主要给零基础小白入门Java开发技术推荐一个靠谱的Java免费视频教程. 零基础小白入门Java免费视频教程推荐-小猿圈 关于Java基础入门视频教程的选择,网上有各种各样的解说,也有各式各样的视频教程,那么我们究竟应该如何选择Java免费视频教程呢?如何才能挑选到合适自己的Java基础入门视频教程呢? 许多想通过观看Java视

教你从零基础小白开始怎么学习C语言

想窥探神秘的代码世界?最好的入口无疑就是C语言. C语言是计算机体系结构的基础,向下可以操作硬件(包括ARM,DSP,单片机等各种微处理器),还可以写驱动,写OS,写编译器.向上可以进一步的学习C++,JAVA等面向对象语言,再学习一些图形用户界面框架,比如Qt,MFC,就可做出类似于计算器.QQ等Window桌面应用,再比如Android,就可以做出微信等Android应用,再比如Unity3D,就可以做出类似王者荣耀.刺激战场等手游.想想是不是就有点小激动呢!最后,如果大家如果在自学遇到困难

零基础小白转行学习前端的学习路线

零基础如何学web前端_从小白到大神进阶:下面给大家介绍一下学习路线:Web前端知识总的可以分为三个部分:初级知识,中级js,高级框架一,初级的主要内容:浏览器的概念介绍: 浏览器的作用.工作方式浏览器的概念介绍: 浏览器的作用.工作方式HTTP协议: 协议产生的原因.作用.及内容页面加载流程:当用户打开浏览器,输入地址栏并按下回车之后发生的事情HTML-CSS-JS基础-jQuery学习内容:HTML及HTML5:理解浏览器如何解析HTML.基本的语法规则.不同标签的使用方式.嵌套方式CSS:

免费领取16套深度学习权威实践课,从零基础小白到大牛

摘要:想学习人工智能却无从下手,听过两三遍网课还是不懂什么是卷积神经网络,好不容易啃完了视频却发现没源码没法调参跑代码,快来AI Studio课程!免费优质课程,系统化学习, 名师指导,提供真实开发案例,在线实训调参,提升学习效果,带你快速入行人工智能! 很多学习AI算法的人都很痛苦,尤其是希望进入大热的深度学习领域的你,是否遇到过以下囧境? 网上有大量的学习资料,质量参差不齐,且不知道知该从何学起: 听过两三遍网课,还是不懂什么是卷积神经网络: 好不容易啃完了视频却发现没源码,没法调参跑代码,