If you add temporarily change your sketch.draw() function to the following..
sketch.draw = function() {
console.log('draw: mouseIsPressed: ' + sketch.mouseIsPressed);
sketch.background(100 + sketch.sin(sketch.millis() * 0.005) * 60);
if(sketch.mouseIsPressed){
Engine.play("entrance");
}
};
and then view your web-browser's Console while running your HTML file you will notice two things:
1. The sketch's draw() function keeps getting called even when the sketch is no long visible.
2. The P5 object is monitoring ALL click events on the page, including ones on the left side-bar.
To fix the first issue you need to call the P5 remove() function which will stop and clear up that P5 object. The following example demonstrates calling the function and also defines a mousePressed() function instead of checking the mouseIsPressed state.
setup.mySketch = function (sketch) {
sketch.setup = function() {
sketch.createCanvas(200, 200);
};
sketch.draw = function() {
sketch.background(100 + sketch.sin(sketch.millis() * 0.005) * 60);
};
sketch.mousePressed = function () {
sketch.remove();
Engine.play("entrance");
}
};
If you want to limit the P5 object to monitoring only the click events on the sketch itself then you need to associate the above mousePressed() function code to the canvas itself. The following example demonstrates how to do this.
setup.mySketch = function (sketch) {
sketch.setup = function() {
var canvas = sketch.createCanvas(200, 200);
canvas.mousePressed(onClick);
};
sketch.draw = function() {
sketch.background(100 + sketch.sin(sketch.millis() * 0.005) * 60);
};
function onClick() {
sketch.remove();
Engine.play("entrance");
};
};