Search code examples
melonjs

MelonJS trigger event manually


I'm making an HTML5 game with melonjs. One of my entitie (called ET1) needs to listen on 2 events : pointerup & pointerdown. My events are registered in my game.js file, as follow :

me.input.registerPointerEvent("pointerup", me.game.viewport, function (event) {
    me.event.publish("pointerup", [ event ]);
});

me.input.registerPointerEvent("pointerdown", me.game.viewport, function (event) {
    me.event.publish("pointerdown", [ event ]);
});

In my ET1 entitie, i'm listening for both of the events :

this.handler = me.event.subscribe("pointerup", function (event) {
...
});

this.handlerDown = me.event.subscribe("pointerdown", function (event) {
...
});

When the pointerdown event is triggered, i'm updating a local property, and if this property reach a certain value, i would like to trigger the pointerup event manually from the update method :

update : function (dt) {
    this.handler = me.event.subscribe("pointerup", function (event) {
         ...
    });

    this.handlerDown = me.event.subscribe("pointerdown", function (event) {
         ...
    });

    localVar++;

    if(localVar > 10){
         // trigger pointerup event
    }
}

I now it's possible with event.publish from withing a registerPointerEvent callback, but i hav no idea how to do this from the entitie update method. Any idea ?


Solution

  • if you don't do anything with the Event object, you can just manually publish the "pointerup" message, same way as you subscribe to them :

    me.event.publish("pointerup", [{}]);
    

    {} here meaning to replace the Event object, although you could pass it as well since it's available in the global scope