# Random sound not working

**URL:** <https://forum.kirupa.com/t/random-sound-not-working/76071>\
**Category:** flash\
**Created:** [January 14, 2005, 2:44pm UTC](https://forum.kirupa.com/t/random-sound-not-working/76071 "2005-01-14T14:44:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![yossarian](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@yossarian](https://forum.kirupa.com/u/yossarian)\
**Post date:** [January 14, 2005, 2:44pm UTC](https://forum.kirupa.com/t/random-sound-not-working/76071/1 "2005-01-14T14:44:11Z")

</div>

Found this code on a thread and thought I could use it for my game (think it’s by Faro…sorry otherwsie to whoever created it)

```auto

  var ranSound = new Sound(this)
  var theKey = new Object();
  theKey.onKeyDown = function() {
  		if (Key.isDown(Key.SPACE)) {
 			 _level0.ranSound.attachSound("s"+Math.round(Math.random()*5))
  		}
  		ranSound.start()
  };
  Key.addListener(theKey);

```

Where I have changed the Linkage to s0, s1, s2, s3 . Sound does play but when the Space Bar is pressed another sound plays \*\*ontop. \*\*Any idea how this can be avoided?

Cheers anyone can help!

Y
