Skip to main content
July 27, 2019
Answered

Playing random sounds in a frame

  • July 27, 2019
  • 4 replies
  • 1262 views

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.

__________________________________________

__________________________________________

This topic has been closed for replies.
Correct answer Nick Gioia

try this code:

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

var mySound=new C();

mySound.play()

4 replies

Nick GioiaCommunity ExpertCorrect answer
Community Expert
July 29, 2019

try this code:

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

var mySound=new C();

mySound.play()

September 22, 2019
Thank you.
JoãoCésar17023019
Community Expert
Community Expert
July 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

July 28, 2019

Hi.

I have changed line 7.

but I get these 2 errors.

JoãoCésar17023019
Community Expert
Community Expert
July 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.

kdmemory
Inspiring
July 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

July 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:

kglad
Community Expert
Community Expert
July 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)

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

kglad
Community Expert
Community Expert
July 28, 2019

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