"ui";
// 创建悬浮窗
var floatyWin = floaty.window(
<vertical padding="5" backgroundColor="#33000000" gravity="center">
<horizontal spacing="0">
<button id="startBtn" text="开始" width="70" height="60" textSize="12sp" margin="0" padding="2"/>
<button id="exitBtn" text="停止" width="70" height="60" textSize="12sp" margin="0" padding="2"/>
</horizontal>
</vertical>
);
// 设置悬浮窗位置
floatyWin.setPosition(device.width - 420, 100);
// 开始运行脚本 循环操作
floatyWin.startBtn.click(function() {
log("开始运行");
}); 运行就会造成hamibot 无响应,给出解决办法,浮窗按钮必须显示,也必须用“ui”;模式
按照你给出的解决代码,运行后hamibot直接黑屏,过一段时间仍然提示 无响应
回答
文档地址 https://docs.hamibot.com/
你遇到的问题可能是由于使用了 ui
模式但没有在 UI 线程中执行界面操作。你需要将 UI 相关的代码放在 ui.run()
中运行,确保对控件的操作都在 UI 线程完成。
以下是修改后的代码示例:
"ui";
ui.run(function () {
// 创建悬浮窗
var floatyWin = floaty.window(
<vertical padding="5" backgroundColor="#33000000" gravity="center">
<horizontal spacing="0">
<button id="startBtn" text="开始" width="70" height="60" textSize="12sp" margin="0" padding="2"/>
<button id="exitBtn" text="停止" width="70" height="60" textSize="12sp" margin="0" padding="2"/>
</horizontal>
</vertical>
);
// 设置悬浮窗位置
floatyWin.setPosition(device.width - 420, 100);
// 开始按钮点击事件
floatyWin.startBtn.click(function () {
log("开始运行");
// 在这里添加你要循环执行的操作
});
// 停止按钮点击事件
floatyWin.exitBtn.click(function () {
log("停止运行");
floatyWin.close();
});
});
注意事项:
- 所有涉及 UI 的操作必须放在
ui.run()
中执行。 - 使用
floaty.window()
创建悬浮窗时,务必注意布局的宽高、边距等设置,避免布局过大或异常导致应用无响应。 - 如果需要进行耗时任务(如循环操作),应开启子线程处理,防止阻塞 UI 线程。例如:
threads.start(function () {
// 在此执行长时间任务
while (true) {
// do something
sleep(1000);
}
});
补充回答 1
文档地址 https://docs.hamibot.com/
你描述的问题是 Hamibot 黑屏并最终无响应,这通常是因为在 UI 线程中执行了耗时操作或资源占用过高,导致主线程卡死。
问题分析
你使用了 "ui"
模式创建悬浮窗,并在按钮点击事件中进行操作。如果点击后执行的是长时间循环、频繁截图、查找图像或颜色等操作,而这些操作又没有放在子线程中运行,就可能导致 Hamibot 主线程被阻塞,从而出现黑屏和无响应的情况。
解决方案
必须将所有耗时操作(如循环、图像处理、sleep)放到子线程中运行,确保不阻塞主线程。
✅ 修改后的代码如下:
"ui";
ui.run(function () {
// 创建悬浮窗
var floatyWin = floaty.window(
<vertical padding="5" backgroundColor="#33000000" gravity="center">
<horizontal spacing="0">
<button id="startBtn" text="开始" width="70" height="60" textSize="12sp" margin="0" padding="2"/>
<button id="exitBtn" text="停止" width="70" height="60" textSize="12sp" margin="0" padding="2"/>
</horizontal>
</vertical>
);
// 设置悬浮窗位置
floatyWin.setPosition(device.width - 420, 100);
// 定义循环标志
var running = false;
// 开始按钮点击事件
floatyWin.startBtn.click(function () {
if (running) return;
running = true;
log("开始运行");
// 启动子线程执行耗时任务
threads.start(function () {
while (running) {
// 示例操作:模拟脚本行为
log("正在运行中...");
sleep(1000); // 每秒执行一次
}
log("任务结束");
});
});
// 停止按钮点击事件
floatyWin.exitBtn.click(function () {
running = false;
log("停止运行");
floatyWin.close();
});
});
关键点说明
问题 | 原因 | 解决方法 |
---|---|---|
黑屏 / 无响应 | 在主线程执行了 while(true) 或 sleep |
使用 threads.start() 开启子线程处理循环逻辑 |
脚本无法交互 | 未正确关闭线程或未释放资源 | 正确设置退出标志(如 running = false ) |
悬浮窗消失 | 没有持续运行的机制 | 若需要长期显示悬浮窗,不要调用 floatyWin.close() |
补充建议
- 如果你要在循环中使用
captureScreen()
或images.findColor()
等图像操作,请确保也放在子线程中。 - 避免在
while(running)
中频繁申请内存或执行高负载操作,否则可能引发系统 ANR(Application Not Responding)。
如有更具体的业务逻辑需求,可以提供详细内容,我可以帮你进一步优化。