最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - p5js button.mousePressed calls function + arguments? - Stack Overflow

programmeradmin7浏览0评论

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!

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!

Share Improve this question edited Feb 20, 2018 at 21:35 H.Scheidl 8554 gold badges11 silver badges25 bronze badges asked Feb 28, 2016 at 9:24 AfzhalAfzhal 511 silver badge2 bronze badges 1
  • 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 Commented Jul 3, 2018 at 3:05
Add a ment  | 

2 Answers 2

Reset to default 6

Use

mousePressed(function() { toggleVideo(1);});

With the latest JS, you can write the following:

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

function toggleVideo(v) {
  blablabla[v].loop();
}  
发布评论

评论列表(0)

  1. 暂无评论