回答
以下是使用 Hamibot 快速监测鲸探 App 颜色变化并自动点击的详细方案:
步骤 1:启动鲸探 App
// 通过应用名称获取包名(更可靠)
var package_name = getPackageName('鲸探');
if (!package_name) {
toastLog('未找到鲸探应用');
exit();
}
// 启动鲸探应用
launchApp(package_name);
步骤 2:循环监测颜色变化
假设抢购按钮的颜色变化特征为:
- 中心颜色:目标颜色(例如
#00FF00
绿色表示可购买) - 周围颜色:特定偏移位置的颜色(如周围像素为灰色
#808080
)
auto.waitFor(); // 请求无障碍服务权限
if (!requestScreenCapture()) {
toastLog('未授权屏幕截图权限');
exit();
}
var targetColor = "#00FF00"; // 目标中心颜色(需根据实际情况修改)
var threshold = 16; // 颜色匹配阈值(数值越小越严格)
// 定义周围颜色检测点(示例:中心点周围偏移 [5,5] 和 [10,10] 的位置)
var offsets = [
[5, 5, "#808080"], // 偏移坐标及颜色
[-5, -5, "#808080"]
];
while (true) {
var img = captureScreen(); // 截取当前屏幕
var point = images.findMultiColors(
img,
targetColor,
offsets,
{ threshold: threshold }
);
if (point) {
toastLog("检测到颜色变化!坐标:" + point.x + ", " + point.y);
click(point.x, point.y); // 点击坐标
exit(); // 购买成功后退出脚本
}
// 降低 CPU 占用(可调整时间,如 500 毫秒)
sleep(500);
}
关键优化点
-
颜色参数调试:
- 中心颜色 (
targetColor
):用截图工具(如 Hamibot 的截图功能)获取按钮变化后的精确颜色值。 - 周围颜色 (
offsets
):添加周围像素的颜色检测,避免误触发(例如按钮周围可能有固定边框颜色)。
- 中心颜色 (
-
性能优化:
- 减少
sleep()
时间:将sleep(500)
调整为更小值(如sleep(100)
),但需权衡 CPU 占用。 - 限定检测区域:通过
region
参数缩小找色范围(例如只检测屏幕下半部分):var region = [0, 1000, 1080, 1500]; // 根据实际界面调整坐标 var point = images.findMultiColors(img, targetColor, offsets, { region: region, threshold: threshold });
- 减少
-
防误触机制:
- 添加二次确认逻辑(如连续检测到颜色变化多次后再点击):
var confirmCount = 0; if (point) { confirmCount++; if (confirmCount >= 3) { // 连续检测 3 次后确认 click(point.x, point.y); exit(); } } else { confirmCount = 0; }
- 添加二次确认逻辑(如连续检测到颜色变化多次后再点击):
完整脚本示例
// 启动鲸探 App
var package_name = getPackageName('鲸探');
if (!package_name) {
toastLog('未找到鲸探应用');
exit();
}
launch(package_name);
auto.waitFor();
if (!requestScreenCapture()) {
toastLog('未授权屏幕截图权限');
exit();
}
var targetColor = "#00FF00"; // 替换为目标颜色
var offsets = [
[5, 5, "#808080"], // 根据实际修改
[-5, -5, "#808080"]
];
var threshold = 16;
var region = [0, 1000, 1080, 1500]; // 根据界面调整区域
toastLog("开始监测...");
while (true) {
var img = captureScreen();
var point = images.findMultiColors(
img,
targetColor,
offsets,
{ region: region, threshold: threshold }
);
if (point) {
toastLog("检测到
> 本回答由 AI 生成,内容仅供参考
ℹ️ 本回答由 AI 生成,内容仅供参考