Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 在ActionScript 3中未满足游戏结束条件时,如何循环播放背景音乐?_Actionscript 3_While Loop_Flash Cs6_Background Music - Fatal编程技术网

Actionscript 3 在ActionScript 3中未满足游戏结束条件时,如何循环播放背景音乐?

Actionscript 3 在ActionScript 3中未满足游戏结束条件时,如何循环播放背景音乐?,actionscript-3,while-loop,flash-cs6,background-music,Actionscript 3,While Loop,Flash Cs6,Background Music,我是相当新的行动脚本,需要知道如何循环BG音乐。我知道其他语言,你可以这样做 while(!endGame){ sound.play(); } 但我似乎找不到如何在AS中实现这一点的语法,或者可能有更好的方法?这是一个7秒的剪辑,所以我需要它继续循环,直到满足结束条件。AS3提供了两种方法来处理这个问题。最简单的方法是使用play命令的loops参数告诉play命令希望声音重复的次数。 第二种方法——它给你更多的控制权——有点复杂。基本上,您启动声音并添加一个监听

我是相当新的行动脚本,需要知道如何循环BG音乐。我知道其他语言,你可以这样做

    while(!endGame){
          sound.play();
}

但我似乎找不到如何在AS中实现这一点的语法,或者可能有更好的方法?这是一个7秒的剪辑,所以我需要它继续循环,直到满足结束条件。

AS3提供了两种方法来处理这个问题。最简单的方法是使用play命令的loops参数告诉play命令希望声音重复的次数。

第二种方法——它给你更多的控制权——有点复杂。基本上,您启动声音并添加一个监听器来监视声音的播放。如果它播放完一个声音_COMPLETE事件,它的回调函数可以重新开始播放。 举个例子:

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;

    public class Main extends Sprite
    {
        private var sound:Sound;
        private var soundChannel:SoundChannel;

        public function Main():void
        {
            if (stage)
                init();
            else
                addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            sound = new Sound();
            var urlRequest:URLRequest = new URLRequest("sound.mp3");
            sound.load(urlRequest);
           soundChannel = sound.play();
           soundChannel.addEventListener(Event.SOUND_COMPLETE, soundFinished);

        }

        public function soundFinished(event:Event):void
        {
            soundChannel.removeEventListener(Event.SOUND_COMPLETE, soundFinished);
            soundChannel = sound.play();
            soundChannel.addEventListener(Event.SOUND_COMPLETE, soundFinished);
        }
    }
}

AS3提供了两种方法来处理这个问题。最简单的方法是使用play命令的loops参数告诉play命令希望声音重复的次数。

第二种方法——它给你更多的控制权——有点复杂。基本上,您启动声音并添加一个监听器来监视声音的播放。如果它播放完一个声音_COMPLETE事件,它的回调函数可以重新开始播放。 举个例子:

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;

    public class Main extends Sprite
    {
        private var sound:Sound;
        private var soundChannel:SoundChannel;

        public function Main():void
        {
            if (stage)
                init();
            else
                addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            sound = new Sound();
            var urlRequest:URLRequest = new URLRequest("sound.mp3");
            sound.load(urlRequest);
           soundChannel = sound.play();
           soundChannel.addEventListener(Event.SOUND_COMPLETE, soundFinished);

        }

        public function soundFinished(event:Event):void
        {
            soundChannel.removeEventListener(Event.SOUND_COMPLETE, soundFinished);
            soundChannel = sound.play();
            soundChannel.addEventListener(Event.SOUND_COMPLETE, soundFinished);
        }
    }
}