Highlighted

Playing random sounds in a frame

New Here ,
Jul 27, 2019

Copy link to clipboard

Copied

I'm trying to get 1 random sound in a set of 3 sounds to play in this one frame. (Like it will choose randomly to play one out of the 3 sounds).

I have right-clicked my selected frame I have used this code in actions:

__________________________________________

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

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

var s=new S();

s.play();

__________________________________________

__________________________________________

But I get these errors:

Symbol 'sprite1', Layer 'actions' Frame 6, Line 4 | 1180: Call to a possibly undefined method getDefinitionByName.

C:\Users\Daniel\Desktop\New folder (2)\gametest.as | 1180: Call to a possibly undefined method addFrameScript.

Symbol 'sprite2', Layer 'Layer 7', Frame 3, Line 1 | 1180: Call to a possibly undefined method gotoAndPlay.

__________________________________________

__________________________________________

Adobe Community Professional
Correct answer by PickleNick28 | Adobe Community Professional

try this code:

var C:Class=Class(getDefinitionByName("music_"+(1+Math.floor(4*Math.random()))));

var mySound=new C();

mySound.play()

Views

441

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 in a frame

New Here ,
Jul 27, 2019

Copy link to clipboard

Copied

I'm trying to get 1 random sound in a set of 3 sounds to play in this one frame. (Like it will choose randomly to play one out of the 3 sounds).

I have right-clicked my selected frame I have used this code in actions:

__________________________________________

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

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

var s=new S();

s.play();

__________________________________________

__________________________________________

But I get these errors:

Symbol 'sprite1', Layer 'actions' Frame 6, Line 4 | 1180: Call to a possibly undefined method getDefinitionByName.

C:\Users\Daniel\Desktop\New folder (2)\gametest.as | 1180: Call to a possibly undefined method addFrameScript.

Symbol 'sprite2', Layer 'Layer 7', Frame 3, Line 1 | 1180: Call to a possibly undefined method gotoAndPlay.

__________________________________________

__________________________________________

Adobe Community Professional
Correct answer by PickleNick28 | Adobe Community Professional

try this code:

var C:Class=Class(getDefinitionByName("music_"+(1+Math.floor(4*Math.random()))));

var mySound=new C();

mySound.play()

Views

442

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
Jul 27, 2019 0
Adobe Community Professional ,
Jul 28, 2019

Copy link to clipboard

Copied

what do you mean by '...right-clicked my selected frame'?

that would trigger a context menu.  just click a keyframe and add your code to the actions panel.  (and you need to import flash.utils.getDefinitionByName)

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...
Jul 28, 2019 0
New Here ,
Jul 28, 2019

Copy link to clipboard

Copied

oh, my mistake, I meant to say 'I right-clicked my selected frame and clicked actions'

I'm not sure what you meant by 'import flash.utils.getDefinitionByName'.

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...
Jul 28, 2019 0
Adobe Community Professional ,
Jul 28, 2019

Copy link to clipboard

Copied

you’re using actionscript.  there’s no frame 0.

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...
Jul 28, 2019 0
Advocate ,
Jul 28, 2019

Copy link to clipboard

Copied

Hi Funky Dragon

I would use something like this:

import flash.media.Sound;

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

function selectAndPlaySound():void {

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

    var oundNow: Sound;

    soundNow = new soundsArr[randNum];

    soundNow.play();

}

whereby I import the sound files in question into the Library and supply them with Linkage names: sound1, sound2, sound3 .

Now you can trigger selectAndPlaySound() from (almost) anywhere.

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...
Jul 28, 2019 1
New Here ,
Jul 28, 2019

Copy link to clipboard

Copied

Hi kdmemory. I have putted the code, and putted the linkage for my sounds.

The 3 errors earlier went away.

However I'm getting 2 errors:

screenshot-error.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...
Jul 28, 2019 0
Adobe Community Professional ,
Jul 28, 2019

Copy link to clipboard

Copied

Hi.

I tried your code and it works. Maybe you have for some reason to import the getDefinitionByName class as suggested by kglad.

Also, I think it's a better idea to follow Klaus approach.

Just change line 7 of Klaus code from

var oundNow: Sound;

to

var soundNow: Sound;

Regards,

JC

__________________________________________
HTML5, JSFL, and AS3 samples: http://bit.ly/2mJgDoG

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...
Jul 28, 2019 0
New Here ,
Jul 28, 2019

Copy link to clipboard

Copied

Hi.

I have changed line 7.

but I get these 2 errors.

screenshot-error.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...
Jul 28, 2019 0
Adobe Community Professional ,
Jul 28, 2019

Copy link to clipboard

Copied

Hi.

Are you placing your code inside of a symbol instance, right?

Is your symbol a Sprite or a MovieClip?

Because Sprites don't have a timeline and you can't add frame scripts to them.

__________________________________________
HTML5, JSFL, and AS3 samples: http://bit.ly/2mJgDoG

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...
Jul 28, 2019 0
New Here ,
Jul 28, 2019

Copy link to clipboard

Copied

It is a MovieClip, yes.

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...
Jul 28, 2019 0
Adobe Community Professional ,
Jul 28, 2019

Copy link to clipboard

Copied

OK.

Can you provide a link to your files?

__________________________________________
HTML5, JSFL, and AS3 samples: http://bit.ly/2mJgDoG

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...
Jul 28, 2019 0
Adobe Community Professional ,
Jul 29, 2019

Copy link to clipboard

Copied

try this code:

var C:Class=Class(getDefinitionByName("music_"+(1+Math.floor(4*Math.random()))));

var mySound=new C();

mySound.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...
Jul 29, 2019 0
New Here ,
Sep 21, 2019

Copy link to clipboard

Copied

Thank you.

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...
Sep 21, 2019 0