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.
__________________________________________
__________________________________________
try this code:
var C:Class=Class(getDefinitionByName("music_"+(1+Math.floor(4*Math.random()))));
var mySound=new C();
mySound.play()
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)
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'.
Copy link to clipboard
Copied
you’re using actionscript. there’s no frame 0.
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
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:
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
Copy link to clipboard
Copied
Hi.
I have changed line 7.
but I get these 2 errors.
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.
Copy link to clipboard
Copied
It is a MovieClip, yes.
Copy link to clipboard
Copied
OK.
Can you provide a link to your files?
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()
Copy link to clipboard
Copied