5

probably silly question but is this possible or not in p5js?

function setup() {
   myButton.mousePressed(toggleVideo(1)); //This toggleVideo works well without argument
}

function toggleVideo(v) {
    blablabla[v].loop();
}  

Many thanks!

H.Scheidl
  • 815
  • 3
  • 11
  • 25
Afzhal
  • 51
  • 1
  • 2
  • The reason yours doesn’t work is because mousePressed takes in the variable that references the function, namely, toggleVideo. By putting the parentheses you are actually calling the function which executes when reached in setup – Moshe Goldberg Jul 03 '18 at 03:05

2 Answers2

6

Use

mousePressed(function() { toggleVideo(1);});
H.Scheidl
  • 815
  • 3
  • 11
  • 25
0

With the latest JS, you can write the following:

function setup() {
 myButton.mousePressed(() => {
    toggleVideo(1)
 });
}

function toggleVideo(v) {
  blablabla[v].loop();
}