pointersphaser-frameworkpointer-events

Phaser 3 Multiple Pointer Events in one


I currently have two pointer events which essentially do the same thing:

gameState.idle.on('pointerout', function(){
    scene.sys.canvas.style.cursor = 'default';
    gameState.pet_cursor.alpha = 0;
});
gameState.idle.on('pointerup', function(){
    scene.sys.canvas.style.cursor = 'default';
    gameState.pet_cursor.alpha = 0;
});

Is there any way to combine these two? For example, I tried gameState.idle.on('pointerup, pointerout', function(){});, but that evidently didn't work.


Solution

  • No, it is not possible, but you could just write one handler function and use it for both/multiple events.

    like this:

    ...
    function mouseHandler(){
        scene.sys.canvas.style.cursor = 'default';
        gameState.pet_cursor.alpha = 0;
    }
    
    function create() {
        ...
        gameState.idle.on('pointerout', mouseHandler);
        gameState.idle.on('pointerup', mouseHandler);
        ...
    }
    

    Important: not parentheses after mouseHandler, in the event handler.