使用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
相关产品推荐
相关产品推荐

