将bounds() 存到storages中,再从storages中取出,如何点击

已存档
兔如花
9
· 1 个月前
第 1 条补充
·
1 个月前

以上解答包错,storedBounds不存在centerX()方法

回答

Hamibot AI
1
· 1 个月前

可以使用 storages 对象来存储和获取 bounds() 的值,然后利用这些值进行点击操作。以下是一个示例代码:

// 获取控件的 bounds 值并存储到 storages 中
var b = text('Hamibot').findOne().bounds();
storages.put('controlBounds', b);

// 从 storages 中取出 bounds 值
var storedBounds = storages.get('controlBounds');

// 使用 bounds 值进行点击
click(storedBounds.centerX(), storedBounds.centerY());

这段代码首先获取了一个文本为 "Hamibot" 的控件的边界信息,并将其存储到 storages 中。然后从 storages 中读取该边界信息,并使用 click 函数在该控件的中心位置进行点击。


补充回答 1

你提到的问题是正确的,bounds() 返回的是一个 Rect 对象,而 storages 无法直接存储 Rect 对象,因此需要将 bounds 的信息以其他形式(如 JSON 字符串)存储。以下是修正后的代码:

// 获取控件的 bounds 值并转换为 JSON 字符串存储到 storages 中
var b = text('Hamibot').findOne().bounds();
storages.put('controlBounds', JSON.stringify(b));

// 从 storages 中取出 bounds 值并解析为对象
var storedBounds = JSON.parse(storages.get('controlBounds'));

// 使用 bounds 值进行点击
click(storedBounds.centerX, storedBounds.centerY);

说明:

  1. 存储时:使用 JSON.stringify(b)bounds 对象转换为字符串。
  2. 取出时:使用 JSON.parse() 将字符串还原为对象。
  3. 点击时:通过 storedBounds.centerXstoredBounds.centerY 获取中心坐标。

这样就可以正确地存储和使用 bounds() 的值了。

ℹ️ 本回答由 AI 生成,内容仅供参考