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

Playing random sounds in a frame

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

__________________________________________

__________________________________________

Views

741

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
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()

Votes

Translate

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

Votes

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
community guidelines
New Here ,
Jul 28, 2019 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'.

Votes

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

Copy link to clipboard

Copied

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

Votes

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

Votes

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
community guidelines
New Here ,
Jul 28, 2019 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

Votes

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

Votes

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
community guidelines
New Here ,
Jul 28, 2019 Jul 28, 2019

Copy link to clipboard

Copied

Hi.

I have changed line 7.

but I get these 2 errors.

screenshot-error.png

Votes

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

Votes

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
community guidelines
New Here ,
Jul 28, 2019 Jul 28, 2019

Copy link to clipboard

Copied

It is a MovieClip, yes.

Votes

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

Copy link to clipboard

Copied

OK.

Can you provide a link to your files?

Votes

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
community guidelines
Community Expert ,
Jul 29, 2019 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()

Votes

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
community guidelines
New Here ,
Sep 21, 2019 Sep 21, 2019

Copy link to clipboard

Copied

LATEST
Thank you.

Votes

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
community guidelines