p5.js

How to detect a click on a sprite in p5play?


I'm using q5.js with p5play. I cannot find an easy way to detect a mouse click on a specific sprite. Whats the 'standard' way to do it in p5play?


Solution

  • Seems like, there is no click event, and one need to put input handling in a global update function that will be called by p5play for every frame. This code allows setting an onMouseLeftClick event handler on any sprite:

    function update() {
        if ( !mouse.presses('left') )
            return
        for ( let s of allSprites )
            if ( s.onMouseLeftClick !== undefined && s.mouse.hovering() )
                s.onMouseLeftClick()
    }