# Actionscripted sound fade

**URL:** <https://forum.kirupa.com/t/actionscripted-sound-fade/172870>\
**Category:** Uncategorized\
**Created:** [December 19, 2005, 4:28am UTC](https://forum.kirupa.com/t/actionscripted-sound-fade/172870 "2005-12-19T04:28:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![master\_phat](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@master\_phat](https://forum.kirupa.com/u/master_phat)\
**Post date:** [December 19, 2005, 4:28am UTC](https://forum.kirupa.com/t/actionscripted-sound-fade/172870/1 "2005-12-19T04:28:17Z")

</div>

```auto

function fadeSound(sound:Sound, fadeTo:Number):Void {
    var tmp:MovieClip = _root.createEmptyMovieClip("tmp_sound_fade"+Math.round(Math.random()*10000000), _root.getNextHighestDepth());
    if (sound == null) return;
    var diff:String;
    if (fadeTo<sound.getVolume()) diff = "l";
    if (fadeTo>sound.getVolume()) diff = "m";
    tmp.onEnterFrame = function():Void {
        switch (diff) {
            case "l" :
                sound.setVolume(sound.getVolume()-1);
                break;
            case "m" :
                sound.setVolume(sound.getVolume()+1);
                break;
        }
        if (sound.getVolume() == fadeTo) {
            delete this.onEnterFrame;
            this.removeMovieClip();
        }
    }
}

var mySound:Sound = new Sound();
mySound.attachSound("aww");
mySound.start();
fadeSound(mySound, 75);

_root.onMouseDown = function() {
    fadeSound(mySound, 85);
}

var mySound2:Sound = new Sound(this);
mySound2.attachSound("thememusicyo");
mySound2.start();
fadeSound(mySound2, 25);

```

for some reason this code I have isnt working correctly… anyone know of a solution? ![](http://kirupa.com/forum/images/smilies/freakyhair.gif)
