> For the complete documentation index, see [llms.txt](https://docs.decentraland.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/an-niu-shi-jian/advanced-button-events.md).

# 高级按钮事件

了解如何处理场景中的用户点击。

如果你需要让场景中的交互遵循与 [注册回调](/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/an-niu-shi-jian/register-callback.md) 或 [基于系统](/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/an-niu-shi-jian/system-based-events.md) 的方法，你可以直接处理原始输入事件数据。这种方式最难，但也最灵活。

## PointerEventsResult 组件

当引擎检测到一个指针事件时，被点击的实体会被分配一个 `PointerEventsResult` 组件。该组件包含有关命中事件的所有原始数据，并存储之前事件的历史数据。

中的辅助函数 `inputSystem`，比如 `inputSystem.isTriggered` 或 `inputSystem.getInputCommand` 对于大多数简单场景都很适用，但如果你需要获取有关命中事件的更多细节，请检查 `PointerEventsResult`.

该 `PointerEventsResult` 该组件保存一个指针事件列表，其中每个事件对应一个对象。它最多存储 100 个事件，较新的事件存储在列表末尾。一旦列表长度达到 100，系统就会开始为每个新事件丢弃旧事件。

列表中的每个事件都有以下数据：

* `analog`: 可选数字，仅存在于来自模拟输入（如摇杆）的事件中，用于存储输入的模拟值。
* `按钮连接您的钱包`: 按下的是哪个按钮 ID。该数字对应于 `InputAction` 枚举，该枚举列出了所有可用按钮。
* `state`: 指针事件类型，来自枚举 `PointerEventType`. *0* 指的是 `PointerEventType.PET_UP`, *1* 为 `PointerEventType.PET_DOWN`, *2* 为 `PointerEventType.PET_HOVER_ENTER`, *3* 为 `PointerEventType.PET_HOVER_LEAVE`, *4* 为 `PointerEventType.PET_PROXIMITY_ENTER`, *5* 为 `PointerEventType.PET_PROXIMITY_LEAVE`
* `时间戳`: 一个 [Lamport 时间戳](https://en.wikipedia.org/wiki/Lamport_timestamp) 以识别每个按钮事件。

  > 注意：此时间戳并不是基于当前时间编号的。可以把它看作一个从 0 开始的计数器，每发生一个事件就加 1。
* `命中`: 一个对象，其中包含有关命中事件的以下数据：
  * `entityId`：被射线命中的实体的 ID 编号。
  * `meshName`: *字符串* ，表示 3D 模型中被命中的具体网格的内部名称。当 3D 模型由多个网格组成时，这很有用。
  * `globalOrigin`: *Vector3* ，表示射线起始的位置（相对于场景）
  * `方向`: *Vector3* 与射线的方向向量一起，采用全局坐标
  * `位置`: *Vector3* ，表示射线与命中的实体相交的位置（相对于场景）
  * `length`：射线从起点到与实体发生命中的位置之间的长度。
  * `normalHit`: *Vector3* 以及使用归一化方向向量，描述世界空间中命中法线的角度。

```ts
import { engine, PointerEventsResult } from '@dcl/sdk/ecs'

function PointerReadingSystem() {
  for (const [entity] of engine.getEntitiesWith(PointerEventsResult)) {
    const events = PointerEventsResult.get(entity)
    for (const event of events) {
      console.log('POINTER EVENT DATA:', event)
    }
  }
}

engine.addSystem(PointerReadingSystem)
```

## 跟踪玩家移动

在实时多人游戏中，如果玩家移动的时机至关重要，你可能希望使用第三方服务器作为事实来源来跟踪每个玩家的位置。你可以通过提前监听按钮并在虚拟形象移动位置之前在服务器中预测其效果来缩短响应时间。

这种方法有助于弥补网络延迟，但必然会导致差异，因此你还应定期轮询玩家当前的位置以进行修正。平衡这些预测和修正可能需要大量微调。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/an-niu-shi-jian/advanced-button-events.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
