Highlighted

Playing random sounds?

Explorer ,
Feb 22, 2019

Copy link to clipboard

Copied

Hello~

I'm a complete newbie to Animate CC, having just finished setting up the visuals for my first flash animation - and now moving on to sound.

I know how to import sounds and add them to frames, but I want have certain sound effects play on certain frames randomly.

Can anyone show me how to do this?

I've been looking around and haven't been able to find an answer myself, and I'm terrible with coding so I can't figure that out myself.

So to be more specific, say I had 3 sounds in my library and I want to have one of those selected randomly and played on frame 30, how would the AS3 code look?

Or is there a simpler way that doesn't involve coding?

Adobe Community Professional
Correct answer by kglad | Adobe Community Professional

assign class names to your sounds eg, 'sound1', 'sound2','sound3' and use:

var soundsA:Array=['sound1','sound2','sound3'];

var S=Class(getDefinitionByName(soundsA[Math.floor(3*Math.random())]));

var s=new S();

s.play();

TOPICS
Discussions

Views

422

Likes

Translate

Translate

Report

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

Playing random sounds?

Explorer ,
Feb 22, 2019

Copy link to clipboard

Copied

Hello~

I'm a complete newbie to Animate CC, having just finished setting up the visuals for my first flash animation - and now moving on to sound.

I know how to import sounds and add them to frames, but I want have certain sound effects play on certain frames randomly.

Can anyone show me how to do this?

I've been looking around and haven't been able to find an answer myself, and I'm terrible with coding so I can't figure that out myself.

So to be more specific, say I had 3 sounds in my library and I want to have one of those selected randomly and played on frame 30, how would the AS3 code look?

Or is there a simpler way that doesn't involve coding?

Adobe Community Professional
Correct answer by kglad | Adobe Community Professional

assign class names to your sounds eg, 'sound1', 'sound2','sound3' and use:

var soundsA:Array=['sound1','sound2','sound3'];

var S=Class(getDefinitionByName(soundsA[Math.floor(3*Math.random())]));

var s=new S();

s.play();

TOPICS
Discussions

Views

423

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 22, 2019 1
Adobe Community Professional ,
Feb 22, 2019

Copy link to clipboard

Copied

assign class names to your sounds eg, 'sound1', 'sound2','sound3' and use:

var soundsA:Array=['sound1','sound2','sound3'];

var S=Class(getDefinitionByName(soundsA[Math.floor(3*Math.random())]));

var s=new S();

s.play();

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 22, 2019 2
Explorer ,
Feb 22, 2019

Copy link to clipboard

Copied

Ahh, great~ :3

So if I also want to add the chance for no sound to play at all, what would I add to that code?

Also, is it possible to alter the odds for individual sounds? Like, say give sounds 1, 2 & 3 a 20% chance each to play on the frame, and a 40% chance for no sound at all to play?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 22, 2019 0
Adobe Community Professional ,
Feb 23, 2019

Copy link to clipboard

Copied

yes:

var probA:Array = [.2,.2,.2,.4];

var soundsA:Array=['sound1','sound2','sound3','no sound'];

var i:int = indexF(Math.random());

if(SoundsA!='no sound'){

var S=Class(getDefinitionByName(soundsA));

var s=new S();

s.play();

}

function indexF(r:Number):int{

var s:Number=0;

for(var i:int=0;i<probA.length;i++){

s+=probA;

if(r<s){

return i;

}

}

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 23, 2019 2
Explorer ,
Feb 23, 2019

Copy link to clipboard

Copied

Hmm, I'm actually getting 3 errors with that second one;

Scene 1, Layer 'Actions', Frame 106, Line 2, Column 5 1151: A conflict exists with definition soundsA in namespace internal.

Scene 1, Layer 'Actions', Frame 106, Line 6, Column 5 1151: A conflict exists with definition S in namespace internal.

Scene 1, Layer 'Actions', Frame 106, Line 7, Column 5 1151: A conflict exists with definition s in namespace internal.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 23, 2019 0
Adobe Community Professional ,
Feb 23, 2019

Copy link to clipboard

Copied

the only problem i see is

if(SoundsA!='no sound'){

should be

if(soundsA!='no sound'){

fix that and retest.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 23, 2019 2
Explorer ,
Feb 23, 2019

Copy link to clipboard

Copied

Nope, same errors again

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 23, 2019 0
Adobe Community Professional ,
Feb 23, 2019

Copy link to clipboard

Copied

then you're probably using the code i suggested in more than one frame.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 23, 2019 2
Explorer ,
Feb 23, 2019

Copy link to clipboard

Copied

Yeah - I have the first code you gave on a frame, and then the second code on a second frame.

Basically I have a looping scene, with the first code always playing a random sound on the first frame (and working fine) - then the second code is placed on another frame about halfway through, with the intent of triggering another sound (from a different set of sounds) mid-loop but this time also having the chance not to play anything.

How should I change the second code it so it would work then?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 23, 2019 1
Adobe Community Professional ,
Feb 23, 2019

Copy link to clipboard

Copied

use different variable names, or if you want to reuse the same variable don't declare and retype it.  eg, if you want to reuse soundsA, omit this line:

var soundsA:Array=['sound1','sound2','sound3','no sound'];

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 23, 2019 2
Explorer ,
Feb 23, 2019

Copy link to clipboard

Copied

Like I said, i'm terrible with coding so i'm really not sure what to do

I tried changing soundsA and probA to soundsB and probB, and that particular error was fixed.

But s & S errors remain - and when I try to change those (to t & T) I get this error;

Scene 1, Layer 'Actions', Frame 106, Line 20, Column 1 1170: Function does not return a value.

This is the code I tried;

var probB:Array = [.2,.2,.2,.4];

var soundsB:Array=['sound4','sound5','sound6','no sound'];

var i:int = indexF(Math.random());

if(soundsB!='no sound'){

var T=Class(getDefinitionByName(soundsB));

var t=new T();

t.play();

}

function indexF(r:Number):int{

var t:Number=0;

for(var i:int=0;i<probB.length;i++){

t+=probB;

if(r<t){

return i;

}

}

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 23, 2019 0
Adobe Community Professional ,
Feb 24, 2019

Copy link to clipboard

Copied

actually that's an erroneous error message (assuming probB elements sum to one), but to resolve the error message use:

function indexF(r:Number):int{

var t:Number=0;

for(var i:int=0;i<probB.length;i++){

t+=probB;

if(r<t){

return i;

}

}

trace("there's an error in probB");

return -1;

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 24, 2019 0
Explorer ,
Feb 24, 2019

Copy link to clipboard

Copied

Ok it's actually letting the flash run now, but I'm getting this error now;

ReferenceError: Error #1065: Variable sound4 is not defined.

I have checked and double-checked, but the class name is correct

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 24, 2019 0
Adobe Community Professional ,
Feb 24, 2019

Copy link to clipboard

Copied

attach a screenshot showing sound4 linkage.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 24, 2019 0
Explorer ,
Feb 24, 2019

Copy link to clipboard

Copied

Like this?

AnimateCCLinkageReq1.png

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 24, 2019 0
Adobe Community Professional ,
Feb 24, 2019

Copy link to clipboard

Copied

yes.  what frame are you using to export?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 24, 2019 0
Explorer ,
Feb 24, 2019

Copy link to clipboard

Copied

You mean like what frame is it on? If so, it's on frame 106~

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 24, 2019 0
Adobe Community Professional ,
Feb 25, 2019

Copy link to clipboard

Copied

no.

tick the export in frame 1 box.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 25, 2019 2
Explorer ,
Feb 25, 2019

Copy link to clipboard

Copied

Ah, fixed! Thank you for all the help~ ❤️

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 25, 2019 0
Adobe Community Professional ,
Feb 25, 2019

Copy link to clipboard

Copied

you're welcome.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 25, 2019 0
Explorer ,
Mar 03, 2019

Copy link to clipboard

Copied

Actually got another quick question;

I figured I can just use "s.play();" in the script of a new frame to repeat the same associated sounds on a different frame loop - however, it only plays one of the sounds rather than all of them randomly as the original one does.

What do I need to add along with s.play(); to make sure it also takes the original randomization into account?

The original code for the sake of convenience;

var soundsA:Array=['sound1','sound2','sound3'];

var S=Class(getDefinitionByName(soundsA[Math.floor(3*Math.random())]));

var s=new S();

s.play();

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 03, 2019 0
Advocate ,
Mar 03, 2019

Copy link to clipboard

Copied

Hi

You have to put the original code into a function (best in frame 1), so that the whole random process gets repeated.

var soundsA: Array = ['sound1', 'sound2', 'sound3'];

var S, s;

function randomizeAndPlay() {

    S = Class(getDefinitionByName(soundsA[Math.floor(3 * Math.random())]));

    s = new S();

    s.play();

}

and then you call in each frame where you want this processed:

randomizeAndPlay();

Klaus

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 03, 2019 0
Adobe Community Professional ,
Mar 03, 2019

Copy link to clipboard

Copied

message 22 will only work if you're using the same array of sound throughout your project.  from previous messages it doesn't look like that's the case: if that's true, you should pass the array to a randomizeAndPlay function:

function randomizeAndPlayF(a:Array):void{

var S:Class = Class(getDefinitionByName(a[Math.floor(a.length* Math.random())])); 

var s = new S(); 

s.play(); 

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 03, 2019 0
Explorer ,
Mar 03, 2019

Copy link to clipboard

Copied

Okay, but what then do I put in the new frame then instead of s.play();?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 03, 2019 0
Adobe Community Professional ,
Mar 03, 2019

Copy link to clipboard

Copied

randomizeAndPlayF([the array of sound classes you want to randomly select and play]);

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 03, 2019 0
Explorer ,
Mar 03, 2019

Copy link to clipboard

Copied

Hmm, doesn't seem to be working.

The flash is compiling and running with no errors, but the first loop with the original code isn't playing any sound at all now, and the second (with just the randomizeAndPlayF) is still only playing one of the sounds.

I decided just to go back to re-using the original code and give everything different variable names. Means I have a lot more variables, but it is working~

Thanks again for trying, anyways. :3

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 03, 2019 0
kglad LATEST
Adobe Community Professional ,
Mar 03, 2019

Copy link to clipboard

Copied

you're welcome.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 03, 2019 0
Advocate ,
Feb 24, 2019

Copy link to clipboard

Copied

Hi ShaadeSilentpaw

Maybe it's a good moment for an alternative suggestion.

First as usual import 3 sound files into your library. Supply them with Linkage names: sound1, sound2 and sound3 .

Then in Actions layer in main timeline / frame 1 write this code:

import flash.media.Sound;

function selectSound() {

    var soundsArr: Array = [sound1, sound2, sound3];

    var randNum = Math.floor(Math.random() * 5);

    var soundNow: Sound;

    if (randNum < 3) {

        soundNow = new soundsArr[randNum];

        soundNow.play();

    }

}

In frame 30 or wherever you want to make it happen simply put (in Actions layer keyframe):

selectSound();

Klaus

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 24, 2019 0