[关闭]
@miniknife 2019-05-13T09:40:55.000000Z 字数 7888 阅读 52079

触动精灵脚本开发实例代码

已不更新,最新版请查看:http://www.touchsprite.com/docs/5489
开发教程


目录

渐开线实现

渐开线在游戏脚本中多用于找怪或者农场类游戏中收割,下面给出两种不同的渐开线在触动精灵中的实现方法:

方形渐开线

  1. init(1)
  2. mSleep(1000)
  3. local x = 1010 --起始坐标x
  4. local y = 698 --起始坐标y
  5. local v = 30 --两点间距离
  6. touchDown(x, y)
  7. mSleep(100)
  8. for var = 1,20 do
  9. j = 0
  10. k = v
  11. for _i = 1,2 do
  12. for i = 1,10 do
  13. x = x + j
  14. y = y + k
  15. touchMove(x, y)
  16. mSleep(20)
  17. end
  18. j = v
  19. k = 0
  20. end
  21. v = v * (-1.05)
  22. end
  23. touchUp(x, y)

圆形渐开线

  1. init(1)
  2. mSleep(1000)
  3. local x0 = 1010 --起始坐标x
  4. local y0 = 698 --起始坐标y
  5. local rr = 20 --设置递增半径
  6. local l = 10 --设置点间距
  7. local p = 0 --初始化角度
  8. local r = 30 --设置首圈半径
  9. local rn = 10 --设置圈数
  10. touchDown(x0, y0)
  11. mSleep(100)
  12. for var = 1,rn do
  13. while p < math.pi * 2 do
  14. x = x0 + r * math.cos(p)
  15. y = y0 - r * math.sin(p)
  16. touchMove(x, y)
  17. mSleep(10)
  18. p = p + l/r
  19. end
  20. p = 0
  21. r = r + rr
  22. end
  23. touchUp(x0, y0)

多点模糊比色

在实际游戏脚本制作中,很多界面单靠1个点不容易进行准确的判断,这里封装一个配合TABLE使用的多点模糊比色函数来实现精确判断:

  1. function multiColor(array,s)
  2. s = math.floor(0xff*(100-s)*0.01)
  3. keepScreen(true)
  4. for var = 1, #array do
  5. local lr,lg,lb = getColorRGB(array[var][1],array[var][2])
  6. local r = math.floor(array[var][3]/0x10000)
  7. local g = math.floor(array[var][3]%0x10000/0x100)
  8. local b = math.floor(array[var][3]%0x100)
  9. if math.abs(lr-r) > s or math.abs(lg-g) > s or math.abs(lb-b) > s then
  10. keepScreen(false)
  11. return false
  12. end
  13. end
  14. keepScreen(false)
  15. return true
  16. end
  17. --用法
  18. g_t_Table = {
  19. { 1962, 52, 0xefdccf},
  20. { 2150, 50, 0xefd8d0},
  21. { 1964, 76, 0xe9d1c5},
  22. { 2152, 74, 0xefdcd1},
  23. { 2122, 62, 0xf1ddd1},
  24. { 2146, 1080, 0x893824},
  25. { 1840, 1082, 0x593724},
  26. }
  27. if multiColor(g_t_Table,90) then
  28. touchDown(100,100)
  29. mSleep(50)
  30. touchUp(100,100)
  31. end
  1. 参数 s 为模糊度,范围 0 - 100,一般使用90即可。
  2. 实例中的TABLE格式可使用触动精灵抓色器生成。

随机字符串

产生随机种子

由于lua中的随机函数产生的随机数是伪随机,我们需要设置一个随机种子来解决此问题

  1. --改进方法
  2. math.randomseed(tostring(os.time()):reverse():sub(1, 6)) -- 随机种子
  3. dialog(math.random())
  1. --进阶方法,需加载扩展库中的socket模块
  2. local ts = require("ts")
  3. local function get_seed()
  4. local t = string.format("%f", socket.gettime())
  5. local st = string.sub(t, string.find(t, "%.") + 1, -1)
  6. return tonumber(string.reverse(st))
  7. end
  8. math.randomseed(get_seed())
  9. for var = 1,5 do
  10. dialog(math.random())
  11. end

自定义字符串随机

  1. function randomStr(str, num)
  2. local ret =''
  3. for i = 1, num do
  4. local rchr = math.random(1, string.len(str))
  5. ret = ret .. string.sub(str, rchr, rchr)
  6. end
  7. return ret
  8. end
  9. --用法
  10. math.randomseed(tonumber(tostring(os.time()):reverse():sub(1,6))) --设置随机种子
  11. for var = 1,5 do
  12. s = randomStr("abcdefghijklmnopqrstuvwxyz", 6) --生成6位随机字母
  13. nLog(s)
  14. end

随机大小写字母

  1. function rndLetter(num)
  2. local ret = ""
  3. pcall(function()
  4. for var = 1,num do
  5. if math.random()>0.5 then
  6. ret = ret..string.char(math.random(65,90))
  7. else
  8. ret = ret..string.char(math.random(97,122))
  9. end
  10. end
  11. end)
  12. return ret
  13. end
  14. --用法
  15. math.randomseed(tonumber(tostring(os.time()):reverse():sub(1,6))) --设置随机种子
  16. for var = 1,5 do
  17. nLog(rndLetter(10)) --生成一个10位随机大小写字母的字符串
  18. end

日期与时间

获取今天是星期几

  1. local today = tonumber(os.date("%w",os.time()))
  2. if today == 0 or today == 6 then --如果是周日或者周六
  3. dialog(today, 0)
  4. end

获取当前日期及时间

  1. local nowTime = os.date("*t",os.time()) --返回一个table
  2. dialog(nowTime.year, 0) --年
  3. dialog(nowTime.month, 0) --月
  4. dialog(nowTime.day, 0) --日
  5. dialog(nowTime.hour, 0) --小时
  6. dialog(nowTime.min, 0) --分钟
  7. dialog(nowTime.sec, 0) --秒钟
  8. dialog(nowTime.yday, 0) --显示当前为一年中的第几天
  9. --时间戳格式化当前时间
  10. local nowTime = os.date("%Y-%m-%d %H:%M:%S", os.time())
  11. dialog(nowTime,0)

UI

一个包含所有控件的默认样式UI实例

  1. local ts = require("ts")--写 showUI 前必须插入这一句
  2. local json = ts.json--写 showUI 前必须插入这一句
  3. w,h = getScreenSize();
  4. MyTable = {
  5. ["style"] = "default",
  6. ["width"] = w,
  7. ["height"] = h,
  8. ["config"] = "save_01.dat",
  9. ["timer"] = 99,
  10. ["orient"] = 0,--需引擎版本 iOS v2.2.5以上版本支持,Android 暂不支持
  11. ["pagetype"] = "multi",--需引擎版本 iOS v2.2.5,Android v2.1.5 以上版本支持
  12. ["title"] = "触动精灵脚本配置",--需引擎版本 iOS v2.2.5,Android v1.2.4 以上版本支持
  13. ["cancelname"] = "取消",
  14. ["okname"] = "开始",
  15. pages =
  16. {
  17. {
  18. {
  19. ["type"] = "Label",
  20. ["text"] = "第一页设置",
  21. ["size"] = 25,
  22. ["align"] = "center",
  23. ["color"] = "0,0,0",
  24. },
  25. {
  26. ["type"] = "RadioGroup",
  27. ["list"] = "选项1,选项2,选项3,选项4,选项5,选项6,选项7",
  28. ["select"] = "1",
  29. },
  30. },{
  31. {
  32. ["type"] = "Label",
  33. ["text"] = "第二页设置",
  34. ["size"] = 25,
  35. ["align"] = "center",
  36. ["color"] = "0,0,0",
  37. },
  38. {
  39. ["type"] = "Edit",
  40. ["prompt"] = "请输入一个字母",
  41. ["text"] = "默认值",
  42. ["kbtype"] = "ascii",
  43. },
  44. {
  45. ["type"] = "Edit",
  46. ["prompt"] = "请输入一个数字",
  47. ["text"] = "默认值",
  48. ["kbtype"] = "number",
  49. },
  50. },{
  51. {
  52. ["type"] = "Label",
  53. ["text"] = "第三页设置",
  54. ["size"] = 25,
  55. ["align"] = "center",
  56. ["color"] = "0,0,0",
  57. },
  58. {
  59. ["type"] = "CheckBoxGroup",
  60. ["list"] = "选项1,选项2,选项3,选项4,选项5,选项6,选项7",
  61. ["select"] = "3@5",
  62. },
  63. {
  64. ["type"] = "ComboBox",
  65. ["list"] = "选项1,选项2,选项3",
  66. ["select"] = "1",
  67. ["data"] = "子选项1,子选项2,子选项3,子选项4#子选项5,子选项6,子选项7#子选项8,子选项9",
  68. ["source"] = "test"
  69. },
  70. {
  71. ["type"] = "ComboBox",
  72. ["select"] = "1",
  73. ["dataSource"] = "test"
  74. },
  75. }
  76. }
  77. }
  78. local MyJsonString = json.encode(MyTable);
  79. retTable = {showUI(MyJsonString)};
  80. for var = 1,#retTable do
  81. nLog(retTable[var]) --输出每一个返回值
  82. end
  1. 以上实例中使用了两个 ComboBox 控件,并在两个 ComboBox 控件之间建立了数据关联,此控件属性需引擎版本 iOS v2.1.8,Android v1.1.0 以上支持。
  2. 以上实例中的 title 属性需引擎版本 iOS v2.2.5,Android v1.2.4 以上版本支持。
  3. 以上实例中的 pagetype 属性需引擎版本 iOS v2.2.5,Android v2.1.5 以上版本支持。
  4. 以上实例中的 orient 属性需引擎版本 iOS v2.2.5以上版本支持,Android 暂不支持。
  5. 以上实例中最后对于 showUI 的调用将返回一个table。

企业版


注入中控器简单的发账号示例

  1. --[[
  2. 简单的发账号示例
  3. script/account.txt内保存账号密码
  4. --]]
  5. local ok,account = telib:controller_injection(
  6. [[
  7. local f = io.open("script/account.txt", "r")
  8. if f then
  9. local account_tab = {}
  10. local account = f:read()
  11. while account do
  12. table.insert(account_tab,account)
  13. account = f:read()
  14. end
  15. f:close()
  16. if #account_tab > 0 then
  17. local f = io.open("script/account.txt", "w")
  18. if f then
  19. for i = 2,#account_tab do
  20. f:write(account_tab[i].."\n")
  21. end
  22. f:close()
  23. end
  24. return account_tab[1]
  25. else
  26. return false
  27. end
  28. end
  29. return false
  30. ]]
  31. )
  32. assert(ok,account)
  33. if account then
  34. nLog("获取账号:"..account)
  35. toast("获取账号:"..account)
  36. else
  37. nLog("获取账号失败")
  38. toast("获取账号失败")
  39. end
  40. mSleep(1000)

封装中控器读写函数

  1. function writeEEFile(str,path,mode) --写入文件内容,路径,类型,默认路径企业版目录scriptData/playLog.text下,自定义路径请把\改为/。默认类型追加,覆盖第三个参数写"wb"
  2. mode = mode or "a"
  3. local telib = require("ts_enterprise_lib")
  4. assert(telib,"无法引入企业专用库")
  5. if path==nil then
  6. local ok,msg = telib:controller_injection(
  7. [[
  8. require("lfs").mkdir("scriptData")
  9. local f = io.open("scriptData/playLog.text","a")
  10. if f then
  11. f:write("]]..str..[[\r\n")
  12. f:close()
  13. end
  14. ]]
  15. )
  16. else
  17. local ok,msg = telib:controller_injection(
  18. [[
  19. local f = io.open("]]..path..[[","]]..mode..[[")
  20. if f then
  21. f:write("]]..str..[[\r\n")
  22. f:close()
  23. end
  24. ]]
  25. )
  26. end
  27. end
  1. function findEEFile(path,tsType) --读取文件内容,读取方式(stringtable)
  2. path = path or "scriptData/playLog.text"
  3. tsType = tsType or "string"
  4. local telib = require("ts_enterprise_lib")
  5. assert(telib,"无法引入企业专用库")
  6. local ok,account
  7. if tsType=="table" then
  8. ok,account = telib:controller_injection(
  9. [[
  10. local f = io.open("]]..path..[[", "r")
  11. if f then
  12. local account_tab = {}
  13. local account = f:read()
  14. while account do
  15. table.insert(account_tab,account)
  16. account = f:read()
  17. end
  18. f:close()
  19. return account_tab
  20. end
  21. return false
  22. ]]
  23. )
  24. else
  25. ok,account = telib:controller_injection(
  26. [[
  27. local f = io.open("]]..path..[[", "r")
  28. if f then
  29. local account = f:read("*all")
  30. f:close()
  31. return account
  32. end
  33. return false
  34. ]]
  35. )
  36. end
  37. return account
  38. end

附录

已知与触动精灵/帮你玩/小精灵/企业版 iOS 冲突的插件列表

如果您安装了以下插件导致服务使用异常,请在Cydia 中卸载该插件后重新安装客户端。

Stashing/内存修正插件

会导致帮你玩无法注册和登录账号,提示文件获取失败

StatusHUD2 插件

会导致脚本showUI无法弹。

FakeGPS Pro

会导致点击失效或服务无法启动

CCSettings

会导致脚本showUI无法弹出

协奏助手

会导致点击失效

按键精灵iOS

会导致UI弹出后点击【取消】【确定】无反应

xxplugin - coc/cok等

会导致点击失效或服务无法启动

XY苹果助手

会导致点击失效或服务无法启动

Lua math 库

函数名 描述 示例 结果
pi 圆周率 math.pi 3.1415926535898
abs 取绝对值 math.abs(-2012) 2012
ceil 向上取整 math.ceil(9.1) 10
floor 向下取整 math.floor(9.9) 9
max 取参数最大值 math.max(2,4,6,8) 8
min 取参数最小值 math.max(2,4,6,8) 2
pow 计算x的y次幂 math.pow(2,16) 65536
sqrt 开平方 math.sqrt(65536) 256
modf 取整数和小数部分 math.modf(20.12) 20 0.12
randomseed 设随机数种子 math.randomseed(os.time())
random 取随机数 math.random(5,90) 5 ~ 90
rad 角度转弧度 math.rad(180) 3.1415926535898
deg 弧度转角度 math.deg(math.pi) 180
exp e的x次方 math.exp(4) 54.598150033144
log 计算x的自然对数 math.log(54.598150033144) 4
log10 计算10为底,x的对数 math.log10(1000) 3
frexp 将参数拆成 x * (2 ^ y) 的形式 math.frexp(160) 0.625 8
ldexp 计算x * (2 ^ y) math.ldexp(0.625,8) 160
sin 正弦 math.sin(math.rad(30)) 0.5
cos 余弦 math.cos(math.rad(60)) 0.5
tan 正切 math.tan(math.rad(45)) 1
asin 反正弦 math.deg(math.asin(0.5)) 30
acos 反余弦 math.deg(math.acos(0.5)) 60
atan 反正切 math.deg(math.atan(1)) 45

文字点阵识别字库制作工具

  1. 大漠:http://pan.baidu.com/s/1hrVhSag

设备 SSH 连接工具

  1. WinSCP:链接:https://pan.baidu.com/s/1del626 密码:wdcn
  2. 简体中文语言包:链接:https://pan.baidu.com/s/1dmYZL0 密码:jd9z
  3. Putty:链接:https://pan.baidu.com/s/1pMwHRUN 密码:pegn

相关工具运行库

  1. VCredist 2010:https://pan.baidu.com/s/1cSap22 密码:mchk
  2. NET Framework 4:http://www.microsoft.com/zh-cn/download/details.aspx?id=17718

OCR(光学字符识别)相关

  1. 触动精灵本地 OCR 简体中文识别库:https://pan.baidu.com/s/1snhuZyH 密码:hy56
  2. 触动精灵本地 OCR 英文数字识别库:https://pan.baidu.com/s/1pMbdlEV 密码:9ewt

LuaSQLite

http://lua.sqlite.org/index.cgi/doc/tip/doc/lsqlite3.wiki

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注