cotton9
asked on
HELP - Randomly choosing from an Array (repeating each variable 3 times).
I have a list of ten sound files. My script randomly presents one sound every few seconds, using an onClipEvent loop. I have the Soundfiles and the #of Presentations in an array: arrTestTones[soundfile][#o fPres] (SO FAR, I'M OK)
-> Now I need to ensure that in the end, I have presented all ten sounds (a total of 3 times each) in random order.
How can I add a loop to make sure I present each (and every) sound a total of 3 times before moving on to the next frame? This is what I have so far:
// RANDOMLY CHOOSE THE SOUND.
_root.randNum = Math.floor(Math.random() * 10)
// IF THE SOUND HASN'T BEEN PRESENTED 3 TIMES YET, PRESENT IT.
if (_root.arrTestTones[_root. randNum][1 ]) < 3 {
tonePresent = new Sound(this);
tonePresent.attachSound(_r oot.arrTes tTones[_ro ot.randNum ][0]);
tonePresent.start();
_root.arrTestTones[_root.r andNum][1] = _root.arrTestTones[_root.r andNum][1] + 1
}
// WHEN ALL THE SOUNDS HAVE BEEN PRESENTED 3 TIMES EACH,
// gotoAndPlay(4);
-> Now I need to ensure that in the end, I have presented all ten sounds (a total of 3 times each) in random order.
How can I add a loop to make sure I present each (and every) sound a total of 3 times before moving on to the next frame? This is what I have so far:
// RANDOMLY CHOOSE THE SOUND.
_root.randNum = Math.floor(Math.random() * 10)
// IF THE SOUND HASN'T BEEN PRESENTED 3 TIMES YET, PRESENT IT.
if (_root.arrTestTones[_root.
tonePresent = new Sound(this);
tonePresent.attachSound(_r
tonePresent.start();
_root.arrTestTones[_root.r
}
// WHEN ALL THE SOUNDS HAVE BEEN PRESENTED 3 TIMES EACH,
// gotoAndPlay(4);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
By splicing the array, you are deleting from it, so no, you can't reference it! However, I would have thought that you could have another array to track the responses...
Could you have another array with the soundnames in once (10 x items - arrTestTones), and use arrTestTones.indexOf(sound _array[sou ndIndex]) to find the element of this reference array to set whatever value you need?
Or you could create an empty array, and add an element with soundname and response at each index?
I'm not sure what responses you are after, and what data you need, so does this help any?
Could you have another array with the soundnames in once (10 x items - arrTestTones), and use arrTestTones.indexOf(sound
Or you could create an empty array, and add an element with soundname and response at each index?
I'm not sure what responses you are after, and what data you need, so does this help any?
ASKER
I ended up using 2 different arrays, and it works great. Thanks for your help!
Glad to help!
ASKER
For example:
sound1 = [soundfile1, 0] // [soundfile, #ofResponses]
sound2 = [soundfile2, 0]
...
sound10 = [soundfile10, 0]
_root.arrTestTones = [sound1, sound1, sound1, sound2 . . .sound10]
I'll add 1 to _root.arrTestTones[_root.r
If I splice away the array, is there a way to reference it again (with the #ofResponses) later?