?
Solved

HELP - Randomly choosing from an Array (repeating each variable 3 times).

Posted on 2004-10-09
5
Medium Priority
?
168 Views
Last Modified: 2010-04-03
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][#ofPres]    (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(_root.arrTestTones[_root.randNum][0]);
      tonePresent.start();                                                                
        _root.arrTestTones[_root.randNum][1] = _root.arrTestTones[_root.randNum][1] + 1
    }

// WHEN ALL THE SOUNDS HAVE BEEN PRESENTED 3 TIMES EACH,
//      gotoAndPlay(4);
0
Comment
Question by:cotton9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
muso120999 earned 1480 total points
ID: 12267529
Here is some pseudocode:

1. Create an array of sound names (30 in total, the 10 repeated three times)...
       sound_array  = new Array("sound1,"sound2" ... "sound10", "sound1,"sound2" ... "sound10", "sound1,"sound2" ... "sound10");
2. Create a random number from the LENGTH of sound_array (soundIndex) - 0-29 in first instance
3. Play the sound: sound_array[soundIndex]
4. Delete the element of sound_array (sound_array.splice(soundIndex,1); )
5. if LENGTH of sound_array > 0 go back to step 2 and repeat

This way you never need to be trapped in a loop creating random numbers until a correct on is found.
0
 

Author Comment

by:cotton9
ID: 12267800
Thanks for your response. That sounds logical, but I came across an issue.  Even though I don't need to worry about #ofPresentations now - Unfortunately, I still need to use a two-part array to keep track of whether there's a response after each presentation.  

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.randNum][1] if there is a response after the presentation.  

If I splice away the array, is there a way to reference it again (with the #ofResponses) later?
0
 
LVL 10

Expert Comment

by:muso120999
ID: 12267855
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[soundIndex]) 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?
0
 

Author Comment

by:cotton9
ID: 12268680
I ended up using 2 different arrays, and it works great.  Thanks for your help!
0
 
LVL 10

Expert Comment

by:muso120999
ID: 12269544
Glad to help!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question