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

Playing random sounds in a frame

Guest
Jul 27, 2019 Jul 27, 2019

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.

__________________________________________

__________________________________________

1.1K
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

correct answers 1 Correct answer

Community Expert , Jul 29, 2019 Jul 29, 2019

try this code:

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

var mySound=new C();

mySound.play()

Translate
Community Expert ,
Jul 28, 2019 Jul 28, 2019

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)

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
Guest
Jul 28, 2019 Jul 28, 2019

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'.

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
Community Expert ,
Jul 28, 2019 Jul 28, 2019

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

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

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

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
Guest
Jul 28, 2019 Jul 28, 2019

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

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
Community Expert ,
Jul 28, 2019 Jul 28, 2019

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

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
Guest
Jul 28, 2019 Jul 28, 2019

Hi.

I have changed line 7.

but I get these 2 errors.

screenshot-error.png

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
Community Expert ,
Jul 28, 2019 Jul 28, 2019

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.

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
Guest
Jul 28, 2019 Jul 28, 2019

It is a MovieClip, yes.

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
Community Expert ,
Jul 28, 2019 Jul 28, 2019

OK.

Can you provide a link to your files?

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
Community Expert ,
Jul 29, 2019 Jul 29, 2019

try this code:

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

var mySound=new C();

mySound.play()

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
Guest
Sep 21, 2019 Sep 21, 2019
LATEST
Thank you.
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