Search code examples
javascriptjquerysoundmanager2

Soundmanager: MySound is not defined


            soundManager.url = 'swf/';

            soundManager.createSound({
                id: 'mySound',
                url: 'http://localhost/htmlshooter/mp3/gun.mp3',
                autoLoad: true,
                autoPlay: true,
                volume: 100
            });

            function placeimage(){
                var t = $('<img src="img/php/target.png" alt="image" id="' +  Math.floor(Math.random()*55)  + '" onclick="doclickimg(this.id);">');
                $('#div').append(t);
                t.css('left', Math.floor(Math.random()*(800 - t.width())));
                t.css('top', Math.floor(Math.random()*(300 - t.height())));
                setTimeout(placeimage, 2000);
            }

            placeimage();

            function doclickimg(imgid){
                doclickdiv();
                $('#'+imgid).remove();
                // +1 score
            }


            function doclickdiv() {
                mySound.play(); 
                // -1 bullet
            }

Now when i click on my div, the image won't disappear, and it says that MySound, from MySound.play in doclickdiv() isn't defined.

Please help me! Why isn't this working?


Solution

  • You are receiving this error because mySound isn't a defined object yet. You'll probably have better luck with...

    var mySound = soundManager.createSound({
        id: 'mySound',
        url: 'http://localhost/htmlshooter/mp3/gun.mp3',
        autoLoad: true,
        autoPlay: true,
        volume: 100
    });
    

    or...

    function doclickdiv() {
        soundManager.getSoundById('mySound').play(); 
        // -1 bullet
    }