> 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). Agents can also search and read this documentation through its [MCP server](https://docs.decentraland.org/~gitbook/mcp).

# 高级按钮事件

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

如果你需要场景中的交互遵循自定义逻辑，而这些逻辑与 [注册回调](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/an-niu-shi-jian/register-callback.md) 或 [基于系统的](https://docs.decentraland.org/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 `PointerEventsResult` 组件包含一个指针事件列表，每个事件对应一个对象。该列表最多可存储 100 个事件，新事件会添加到列表末尾。列表达到 100 个事件后，每当有新事件到来，就会丢弃最旧的事件。

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

* `analog`：可选数值，仅存在于模拟输入（如操纵杆）事件中，用于存储输入的模拟值。
* `button`：被按下的按钮 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`
* `timestamp`：一个 [Lamport 时间戳](https://en.wikipedia.org/wiki/Lamport_timestamp) ，用于标识每个按钮事件。

  > 注意：此时间戳并非根据当前时间编号。可以将它视为从 0 开始的计数器，每发生一个事件就递增 1。
* `hit`：一个对象，包含以下命中事件数据：
  * `entityId`：被射线命中的实体的 ID 号。
  * `meshName`: *字符串* 包含 3D 模型中被击中的特定网格的内部名称。当 3D 模型由多个网格组成时，这很有用。
  * `globalOrigin`: *Vector3* 表示射线的起点位置（相对于场景）
  * `direction`: *Vector3* ，表示射线的方向向量（全局坐标）
  * `position`: *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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
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=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
