Howler.js - Referencing And Triggering Files From An Array/for-loop
I'm trying to use an array and for-loop to index and name some Howls and Howl trigger buttons. I've referenced this question for what I'm trying to achieve: Howler - Random sound
Solution 1:
There are two issues that I see here:
- You are referencing the variable
i
inside theclick
handler without maintaining scope. Because of this, it will always seei
as the last value. You could usebind
as one way of fixing this:
$('#' + sounds[i] + ' span.ascii-play').on('click', function (i2, e) {
sounds[i2].play();
}.bind(null, i));
- You are trying to call
play
onsounds
, which isn't holding the reference to theHowl
object. You should be callingplay
onhowls[sounds[i2]]
instead.
EDIT: In this case it is just easier to use a forEach
, so I've updated your fiddle to do that and fix the scoping issues here: http://jsfiddle.net/zmjz7sf3/1/.
Post a Comment for "Howler.js - Referencing And Triggering Files From An Array/for-loop"