听起来不行

时间:2014-09-14 06:20:09

标签: actionscript-3 audio

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");
            }
        }
    }

}

我收到一条错误消息:

  

“错误:错误#2037:按错误顺序调用函数,或   早先的电话不成功。错误#2044:未处理的IOErrorEvent:。   text =错误#2032:流错误。 at untitledCode()“。

声音文件声明为SoS。

1 个答案:

答案 0 :(得分:0)

  • 您无需致电load()函数:

      

    如果将有效的URLRequest对象传递给Sound构造函数,则   构造函数自动调用Sound的load()函数   宾语。如果未将有效的URLRequest对象传递给Sound   构造函数,您必须为Sound对象调用load()函数   你自己,或者流不会加载。

         

    在Sound对象上调用load()后,您无法再加载a   将不同的声音文件放入Sound对象中。加载不同的声音   文件,创建一个新的Sound对象。

    Reference

    因此您的代码应如下所示:

    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 var SoS:Sound = new Sound(/* req */);
    public function untitledCode()
    {
        mcPlayer2.addEventListener(Event.ENTER_FRAME, P1hit);
        SoS.addEventListener(Event.COMPLETE, onCompletion);
        SoS.load(req);
    }
    

  • 您应该在路径中使用正斜杠而不是反斜杠:" C:/ Users / Anirudh_2 / Documents / Game(Fl)/ Sword Slash.mp3"