lua的一些封装方法

获取点击屏幕坐标

复制代码
 1 function getTouchPosition()
 2     local locationX, locationY
 3 
 4     local listener = cc.EventListenerTouchOneByOne:create()
 5     listener:setSwallowTouches(true)
 6     listener:registerScriptHandler(function(touch, event)
 7         locationX, locationY = touch:getLocation().x, touch:getLocation().y
 8         return true
 9     end, cc.Handler.EVENT_TOUCH_BEGAN)
10 
11     cc.Director:getInstance():getEventDispatcher():addEventListenerWithSceneGraphPriority(listener, cc.Director:getInstance():getRunningScene())
12 
13     cc.Director:getInstance():getRunningScene():runAction(cc.Sequence:create(cc.DelayTime:create(0.1), cc.CallFunc:create(function() cc.Director:getInstance():getEventDispatcher():removeEventListener(listener) end)))
14 
15     return locationX, locationY
16 end
复制代码

 

  

 

创建了一个事件监听器,并将其注册到事件分发器中。
在监听器的 EVENT_TOUCH_BEGAN 事件中,我们仍然通过调用 touch:getLocation() 方法获取点击位置的x和y坐标,并将它们存储在locationX和locationY变量中。
最后,我们仍然添加了一些代码来确保监听器被及时地从事件分发器中删除,以避免内存泄漏。

 

orc文字识别并进行匹配获取文字坐标
需要先安装 tesseract 
复制代码
 1 local tess = require("tesseract")
 2 tess.init("eng")
 3 
 4 local function findText(imagePath, text)
 5     local result = tess.recognize(imagePath)
 6     for i, line in ipairs(result.lines) do
 7         local startIndex, endIndex = string.find(line.text, text)
 8         if startIndex ~= nil then
 9             local charWidth = line.width / #line.text
10             local x = line.x + (startIndex - 1) * charWidth + charWidth / 2
11             local y = line.y + line.height / 2
12             return {x = x, y = y}
13         end
14     end
15     return nil
16 end
17 
18 local function searchAndPrint(imagePath, text)
19     local result = findText(imagePath, text)
20     if result ~= nil then
21         print(string.format("Found text \"%s\" at position (%d, %d)", text, result.x, result.y))
22         return result
23     else
24         print(string.format("Text \"%s\" not found", text))
25         return nil
26     end
27 end
28 
29 searchAndPrint("test.png", "Hello")
复制代码

 

 

通过监听键盘点击ctrl+1、ctrl+2、ctrl+3、ctrl+4、ctrl+5事件,可以随时开始、停止、继续和重新开始的循环任务
复制代码
 1 -- 定义任务函数
 2 local function myTask()
 3   print("这是一个任务")
 4 end
 5 
 6 -- 定义任务控制模块
 7 local taskCtrl = {
 8   interval = 1,
 9   running = false,
10   thread = nil,
11   start = function(self)
12     self.running = true
13     self.thread = coroutine.create(function()
14       while self.running do
15         myTask()
16         os.sleep(self.interval)
17       end
18     end)
19     coroutine.resume(self.thread)
20   end,
21   stop = function(self)
22     self.running = false
23     if self.thread then coroutine.yield(self.thread) end
24     self.thread = nil
25   end,
26   resume = function(self)
27     if not self.running then self:start()
28     elseif self.thread then coroutine.resume(self.thread) end
29   end,
30   restart = function(self) self:stop() self:start() end
31 }
32 
33 -- 监听键盘事件
34 local keys = {"1","2","3","4","5"}
35 for _, key in ipairs(keys) do
36   keyboard.addHotkey("ctrl+"..key, function()
37     print("按下了 Ctrl+"..key)
38     if taskCtrl[key] then taskCtrl[key](taskCtrl) else os.exit() end
39   end)
40 end
复制代码

 

 
读取和写入本地csv文件
复制代码
 1 local csv = require("csv")
 2 
 3 -- 从 CSV 文件中读取数据
 4 local data = csv.open("data.csv"):read()
 5 
 6 -- 输出读取到的数据
 7 for i, record in ipairs(data) do
 8   for j, field in ipairs(record) do
 9     print(field)
10   end
11 end
12 
13 -- 将数据写入到 CSV 文件中
14 csv.open("data.csv", "w"):write({
15   {"id", "name", "age"},
16   {1, "John", 30},
17   {2, "Jane", 25},
18   {3, "Bob", 45}
19 })
20 
21 
22 
23 
24 --------版本二
25 local csv = require("csv")
26 
27 -- 读取 CSV 文件数据
28 local function read_csv(file)
29   local data = csv.open(file):read()
30   return data
31 end
32 
33 -- 输出 CSV 文件数据
34 local function print_csv(data)
35   for i, record in ipairs(data) do
36     for j, field in ipairs(record) do
37       print(field)
38     end
39   end
40 end
41 
42 -- 写入 CSV 文件数据
43 local function write_csv(file, data)
44   local writer = csv.open(file, {headers=false})
45   writer:write(data)
46   writer:close()
47 end
48 
49 -- 读取 CSV 文件数据
50 local data = read_csv("data.csv")
51 
52 -- 输出 CSV 文件数据
53 print_csv(data)
54 
55 -- 写入 CSV 文件数据
56 write_csv("data.csv", {
57   {"id", "name", "age"},
58   {"1", "John", "30"},
59   {"2", "Jane", "25"},
60   {"3", "Bob", "45"},
61 })
复制代码

 

遍历时获取数据表中第一行第二个值
1 local second_value = data[1][2]
2 
3 
4 for i, record in ipairs(data) do
5   local second_value = record[2]
6   -- do something with second_value
7 end

 

 
 
 
posted @   余额一个亿  阅读(213)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
点击右上角即可分享
微信分享提示