0
votes
package 
{

    import flash.display.MovieClip;
    import flash.events.*;
    import flash.media.Sound;
    import flash.net.URLRequest;

    public class untitledCode extends MovieClip
    {
        private var speed:Number = 5;
        private var req:URLRequest = new URLRequest("C:\Users\Anirudh_2\Documents\Game(Fl)\Sword Slash.mp3");
        private var SoS:Sound = new Sound(req);
        public function untitledCode()
        {
            mcPlayer2.addEventListener(Event.ENTER_FRAME, P1hit);
            SoS.addEventListener(Event.COMPLETE, onCompletion);
            SoS.load(req);
        }
        private function onCompletion(event:Event):void
        {
            stage.addEventListener(KeyboardEvent.KEY_DOWN, whenKey1);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, whenKey2);
        }
        private function whenKey1(event:KeyboardEvent):void
        {
            if (event.keyCode == 38)
            {
                mcPlayer1.y -=  speed;
            }
            if (event.keyCode == 40)
            {
                mcPlayer1.y +=  speed;
            }
            if (event.keyCode == 37)
            {
                mcPlayer1.x -=  speed;
            }
            if (event.keyCode == 39)
            {
                mcPlayer1.x +=  speed;
            }
            if (event.keyCode == 96)
            {
                mcPlayer1.play();
                SoS.play();
            }

        }
        private function whenKey2(event:KeyboardEvent):void
        {
            if (event.keyCode == 87)
            {
                mcPlayer2.y -=  speed;
            }
            if (event.keyCode == 83)
            {
                mcPlayer2.y +=  speed;
            }
            if (event.keyCode == 65)
            {
                mcPlayer2.x -=  speed;
            }
            if (event.keyCode == 68)
            {
                mcPlayer2.x +=  speed;
            }
            if (event.keyCode == 90)
            {
                mcPlayer2.play();
            }

            SoS.play();
        }

        private function P1hit(event:Event):void
        {
            if (mcPlayer1.x >= (mcPlayer2.x - (mcPlayer1.width - 35)))
            {
                //trace("WOW")
                //mcPlayer1.x >= (mcPlayer2.x - (mcPlayer1.width - 35))
            }
            else if (mcPlayer1.hitTestObject(mcPlayer2))
            {
                //trace("All Good");
            }
        }
    }

}

I have an error message saying:

"Error: Error #2037: Functions called in incorrect sequence, or earlier call was unsuccessful. Error #2044: Unhandled IOErrorEvent:. text=Error #2032: Stream Error. at untitledCode()".

The sound file is declared as an SoS.

1

1 Answers

0
votes
  • You dont need to call load() function:

    If you pass a valid URLRequest object to the Sound constructor, the constructor automatically calls the load() function for the Sound object. If you do not pass a valid URLRequest object to the Sound constructor, you must call the load() function for the Sound object yourself, or the stream will not load.

    Once load() is called on a Sound object, you can't later load a different sound file into that Sound object. To load a different sound file, create a new Sound object.

    Reference

    so your code should looks like:

    private var SoS:Sound = new Sound(req);
    public function untitledCode()
    {
        mcPlayer2.addEventListener(Event.ENTER_FRAME, P1hit);
        SoS.addEventListener(Event.COMPLETE, onCompletion);
        //SoS.load(req);
    }
    
    or:
    private var SoS:Sound = new Sound(/* req */);
    public function untitledCode()
    {
        mcPlayer2.addEventListener(Event.ENTER_FRAME, P1hit);
        SoS.addEventListener(Event.COMPLETE, onCompletion);
        SoS.load(req);
    }
    
  • You should use forward slash instead of back slash in the path: "C:/Users/Anirudh_2/Documents/Game(Fl)/Sword Slash.mp3"