We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm trying to create an array of buttons (dom element) which trigger sound files (up to 100). It's difficult for me to figure out how to access each button individually, such that button(index48) returns its own value. This doesn't seem to work:
for (var i=0; i<button.length; i++) {
button[i]=createButton(i);
button[i].mousePressed(print(i));
}
Any tips? thanks
Answers
undefined
value as a callback to p5.Element::mousePressed() method instead of a function! #-oconst und = print();
. :>print('und is : ' + und);
. >-)=>
to turn that into a lambda, and pass that as the callback argument for p5.Element::mousePressed():button[i].mousePressed(() => print(i));
. O:-)var
creates 1 single variable, as we would expect. ~O)++
until it reaches the value button.length: L-)for (var i = 0; i < button.length; ++i) {
.let
(orconst
) in place ofvar
for declaring the iterator variable:for (let i = 0; i < button.length; ++i) {
. :)>-let
, each loop iteration gets a brand new variable i rather than reusing it. :-bdThis is exceptional. Informative and effective! (and the emoji certainly spice things up too!) The fat arrow, is that ES6? I haven't learned about that yet. Thanks a million!
https://Developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions