# Trying to do a random sound on "on (press)" (yes I am a beginer

**URL:** <https://forum.kirupa.com/t/trying-to-do-a-random-sound-on-on-press-yes-i-am-a-beginer/27173>\
**Category:** flash\
**Created:** [July 30, 2003, 8:16pm UTC](https://forum.kirupa.com/t/trying-to-do-a-random-sound-on-on-press-yes-i-am-a-beginer/27173 "2003-07-30T20:16:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Goremata](https://avatars.discourse-cdn.com/v4/letter/g/e19b73/32.png) [@Goremata](https://forum.kirupa.com/u/Goremata)\
**Post date:** [July 30, 2003, 8:16pm UTC](https://forum.kirupa.com/t/trying-to-do-a-random-sound-on-on-press-yes-i-am-a-beginer/27173/1 "2003-07-30T20:16:51Z")

</div>

I am trying to do a random sound on a on (press) event for a button. For the sounds “Be Quiet” and “Be Quiet2” I have modified the linkage and set the indentifier to the same names, also I checked “Export for action script” and “Export in first frame.” When I test the movie and click on the button, no sound happens. I am thinking it must be the script, can anyone help me? There might even be a reward involved. 😉 (well a mediocre award) BTW I am somewhat a beginer in this, so bear with me if I get a tiny bit confused. Here is my current code.

on (press) {  
var X = Math.floor(Math.random() \* (2 - 1)) + 1;  
if (X = 2) {  
mySound.attachSound(“Be Quiet”);  
mySound.start([0]);  
}  
if (X = 1){  
mySound.attachSound(“Be Quiet2”);  
mySound.start([0]);  
}  
}

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 30, 2003, 8:32pm UTC](https://forum.kirupa.com/t/trying-to-do-a-random-sound-on-on-press-yes-i-am-a-beginer/27173/2 "2003-07-30T20:32:08Z")

</div>

Try this tutorial  
[http://www.kirupa.com/developer/mx/loading.htm](http://www.kirupa.com/developer/mx/loading.htm)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 30, 2003, 8:33pm UTC](https://forum.kirupa.com/t/trying-to-do-a-random-sound-on-on-press-yes-i-am-a-beginer/27173/3 "2003-07-30T20:33:41Z")

</div>

haven’t checked your mysound.attachSound code but your random is too complicated. You can use instead:  
on (press) {  
var i = Math.random();  
if(i \> 0.5){play your sound}else{play the other sound}}

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 30, 2003, 8:37pm UTC](https://forum.kirupa.com/t/trying-to-do-a-random-sound-on-on-press-yes-i-am-a-beginer/27173/4 "2003-07-30T20:37:22Z")

</div>

also you don’t need to specify where to start your sound if you want it to start at the begining:

mySound.start();

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 30, 2003, 8:39pm UTC](https://forum.kirupa.com/t/trying-to-do-a-random-sound-on-on-press-yes-i-am-a-beginer/27173/5 "2003-07-30T20:39:39Z")

</div>

[AS]on (press) {  
var X = Math.floor(Math.random()\*2)+1;  
var mySound = new Sound();  
mySound.attachSound(“Be Quiet”+X);  
mySound.start();  
}[/AS]Make your sounds linkage identifiers “Be Quiet1” and “Be Quiet2” (no quotes)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 30, 2003, 11:31pm UTC](https://forum.kirupa.com/t/trying-to-do-a-random-sound-on-on-press-yes-i-am-a-beginer/27173/6 "2003-07-30T23:31:59Z")

</div>

Thanks for your help. I got that random expression from one of the tutorials on this website. Claudio that did the trick, it works great, thank you. And for that reward thingy, to anyone that replied to this messenge (Avi, Uli, and claudio), I can do a logo or simple midi song for them. Just tell me what you would like done. You can see examples of my style at [www.goremata.net](http://www.goremata.net)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 30, 2003, 11:38pm UTC](https://forum.kirupa.com/t/trying-to-do-a-random-sound-on-on-press-yes-i-am-a-beginer/27173/7 "2003-07-30T23:38:59Z")

</div>

Ii didnt even notice about the reward thing.😛  
I must admit sometimes i misread the threads because im so lazy to read everything 😊

Glad i could help, and theres no need to reward 🙂
