Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Random array sound selection problem

New Here ,
Oct 30, 2013 Oct 30, 2013

Hello,

I'm a gigantic AS3 noob so please be gentle with me. I have probably simply done something very stupid.

I have an animated character and a set of footstep sounds, and have set up code so that at a set point in the walk cycle a random footstep sound is played based on frame label selection. The following code is in the character movieclip:

function getRandomLabel():String {

  var labels:Array = new Array("step1", "step2", "step3", "step4","step5", "step6", "step7", "step8");

  var index:Number = Math.floor(Math.random() * labels.length);

  trace (labels[index])

  return labels[index];

}

step1, 2 etc refer to the frame labels of the sound movieclip I have nested inside the character movieclip, which contains the eight sounds on 8 frames with appropriate labels (plus an additional blank frame at the beginning).

At the frames in the character animation where I wish a footstep sound to play, I have the code:

this.soundtest.gotoAndStop(getRandomLabel());

'soundtest' is the name of the sound movieclip containing the footsteps. This works perfectly; I get a random sound at the right time on each footfall. The weird problem is that if the random label index returned is the same as the previous one (i.e. step1 and step1 again) the second sound does not play. This only happens if the same label is repeated, otherwise it works fine. So the footsteps have occasional breaks in them where 'something' prevents repeat sounds/array indices being played. What could be the problem here?

Thanks in advance.

TOPICS
ActionScript
701
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 30, 2013 Oct 30, 2013

If you are going to and stopping at the same frame you are already in you are not going and stopping anywhere, so the sound does not re-execute as if you just entered the frame.

You should look into loading the sound dynamically if you want an approach to have the same sounds repeatable.  That way you don't need to move around a timeline.

If that's too much for you to want to try to tackle (it is not difficult) then an alternative could be to avoid repeating the same sound by putting in some conditional logic in your function that checks if the currently selected sound is a repeat of the one just played, and if so, pick another... until a non-repeat is selected.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 30, 2013 Oct 30, 2013

Thank you for the quick response, and as expected I was missing something obvious. Your explanation makes perfect sense. I'll look into loading the sounds dynamically- I'm already googling, but if you knew of any good examples of the method you suggest (I have no experience with dynamic sound loading... yet!) it would be very helpful.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 30, 2013 Oct 30, 2013

Here is a link to a tutorial that covers most of the basics with using AS3 and the Sound class.  Since you probably already have the files imported into your library you can focus on the second section where it shows how to load internal sounds

http://www.republicofcode.com/tutorials/flash/as3sound/

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 30, 2013 Oct 30, 2013
LATEST

Brilliant, thank you Ned.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines