You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Decentraland SDK触发模拟主输入事件时遇注册错误求助

解决Decentraland模拟主输入(E键)触发报错问题

问题根源

直接用字面量创建LocalActionButtonEvent对象、新建EventManager实例,违反了Decentraland事件系统规则:

  • 内置事件类必须通过构造函数实例化,字面量对象无法被事件管理器识别(未注册对应构造器)
  • 场景需使用全局EventManager实例,而非新建独立实例

修正后的代码

按钮点击设置(无需修改)

let btn = new UIImage(someContainer, new Texture('assets/images/btn.png'))
btn.onClick = () => { triggerPrimaryInput() }

修正后的触发函数

function triggerPrimaryInput() {
    // 通过构造函数创建事件实例,替代字面量写法
    const simulatedEvent = new LocalActionButtonEvent()
    simulatedEvent.origin = new Vector3(0, 0, 0)
    simulatedEvent.direction = new Vector3(0, 0, 0)
    simulatedEvent.button = ActionButton.PRIMARY
    simulatedEvent.buttonId = 1
    simulatedEvent.type = 1

    // 使用全局EventManager实例触发事件
    EventManager.fireEvent(simulatedEvent)
}

额外注意

若场景使用模块化语法,需确保正确导入依赖:

import { LocalActionButtonEvent, ActionButton, EventManager, Vector3 } from '@dcl/sdk/ecs'

内容的提问来源于stack exchange,提问作者franco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 15:35:28