[英]Fire an event with Decentraland SDK
I'm trying to fire a custom event in my Decentraland scene.我正在尝试在我的 Decentraland 场景中触发自定义事件。 According with the docs , I just need to do something like: 根据文档,我只需要做类似的事情:
const events = new EventManager()
events.fireEvent(new MyEvent(field1, field2))
So, following that example I'm trying to simulate the E
user input when users click on a UIImage
.因此,按照该示例,我尝试在用户单击UIImage
时模拟E
用户输入。 To accomplish that:要做到这一点:
let btn = new UIImage(someContainer, new Texture('assets/images/btn.png'))
btn.onClick = () => { triggerPrimaryInput() }
triggerPrimaryInput
function is like: triggerPrimaryInput
function 是这样的:triggerPrimaryInput()
{
const simulatedEvent: LocalActionButtonEvent = {
origin: new Vector3(0, 0, 0),
direction: new Vector3(0, 0, 0),
button: ActionButton.PRIMARY,
buttonId: 1,
type: 1
}
const em = new EventManager
em.fireEvent(simulatedEvent)
}
Error: Error: The EventConstructor is not registered
Because of that, I created a new class using the @EventConstructor()
decorator and I put the triggerPrimaryInput()
function's logic in the constructor, but the error keeps the same.因此,我使用@EventConstructor()
装饰器创建了一个新的 class 并将triggerPrimaryInput()
函数的逻辑放在构造函数中,但错误保持不变。
Well, debuging Decentraland's Input.ts
I finally solve my problem.好吧,调试 Decentraland 的Input.ts
我终于解决了我的问题。 I just needed to do:我只需要做:
// Create the button and set the onClick property
let btn = new UIImage(someContainer, new Texture('assets/images/btn.png'))
btn.onClick = () => { triggerPrimaryInput() }
function triggerPrimaryInput()
{
// Define the input event
const simulatedEvent: LocalActionButtonEvent = {
origin: new Vector3(0, 0, 0),
direction: new Vector3(0, 0, 0),
button: ActionButton.PRIMARY,
buttonId: 1,
type: 1
}
// Call this method using the simulated event
Input.instance.handlePointerEvent(simulatedEvent)
}
With that, I can simulate the E
keyboard input when the user clic over a button.这样,当用户点击按钮时,我可以模拟E
键盘输入。 Using this strategy I was able to create a custom UI allowing the user to interact with an NPC avoiding the use of the dialogs provided by NPC-Utils使用这种策略,我能够创建一个自定义 UI,允许用户与 NPC 交互,避免使用NPC-Utils提供的对话框
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.