0

A problem for ActionScript 3, in CS5

Hi I am making a simple menu (I know it's simple, but my as3 skills aren't that "advanced" yet) and a simple option menu with sound options in which the only thing you can do is change music from 1 through 3, but, my problem begins here. I wanted to make the buttons to interact with each other, and whenever Button 1 is pressed, BGM1Sound would play, and if Button 2 or 3 were pressed before Button 1 the songs on those buttons would stop. But. It's not working. I tried to channel it with SoundChannel, and tried to make it just BGM1.stop(); ; even turned to Boolean, but I was just running in circles, as either it wouldn't work, or I would get errors - which were 1120, the usual for undefined property for both booleans and SoundChannels (which I kind of figured I would get, but hey, it was worth a try). Of course, the music loading works just fine, but the problem, as stated above, is for those to not stop when another button is pressed.

Here's the code, with just buttons and assigned to those respective BGM sounds:

stop();
import flash.events.MouseEvent;
import flash.media.SoundChannel;
import flash.media.Sound;
import PffLib_fla.MainTimeline;

var BGM1ON:Boolean=false;
var BGM2ON:Boolean=false;
var BGM3ON:Boolean=false;
var BGM1Channel:SoundChannel=new SoundChannel();
var BGM2Channel:SoundChannel=new SoundChannel();
var BGM3Channel:SoundChannel=new SoundChannel();

BGM1Btn.addEventListener(MouseEvent.CLICK,handler3);
BGM2Btn.addEventListener(MouseEvent.CLICK,handler3);
BGM3Btn.addEventListener(MouseEvent.CLICK,handler3);
MainMenuBtn2.addEventListener(MouseEvent.CLICK,handler3);

function handler3(CLICKEvent3:MouseEvent):void{
    if(CLICKEvent3.target==MainMenuBtn2){
        gotoAndStop("MainMenu");
    }
    if(CLICKEvent3.target==BGM1Btn){
        BGM2Channel=BGM2Sound.stop();//just going to write those channels here for a reference of what I was doing, but basically, each button had channels referring to each sound, and the idea was to stop the sound not loaded by respective button when clicked//
        BGM3Channel=BGM3Sound.stop();
        var SoundRequest1:Sound=new Sound();
        SoundRequest1.addEventListener(Event.COMPLETE,onSoundLoaded1);
        var Request1:URLRequest=new URLRequest("audio/BGM1.mp3");
        SoundRequest1.load(Request1);

        function onSoundLoaded1(SoundRequestEvent1:Event):void{
            var BGM1Sound:Sound=SoundRequestEvent1.target as Sound;
            BGM1Sound.play(0,1);
            BGM1ON=true;
        }
    }
    if(CLICKEvent3.target==BGM2Btn){
        var SoundRequest2:Sound=new Sound();
        SoundRequest2.addEventListener(Event.COMPLETE,onSoundLoaded2);
        var Request2:URLRequest=new URLRequest("audio/BGM2.mp3");
        SoundRequest2.load(Request2);

        function onSoundLoaded2(SoundRequestEvent2:Event):void{
            var BGM2Sound:Sound=SoundRequestEvent2.target as Sound;
            BGM2Sound.play(0,1);
            BGM2ON=true;
        }
    }
    if(CLICKEvent3.target==BGM3Btn){
        var SoundRequest3:Sound=new Sound();
        SoundRequest3.addEventListener(Event.COMPLETE,onSoundLoaded3);
        var Request3:URLRequest=new URLRequest("audio/BGM3.mp3");
        SoundRequest3.load(Request3);

        function onSoundLoaded3(SoundRequestEvent3:Event):void{
            var BGM3Sound:Sound=SoundRequestEvent3.target as Sound;
            BGM3Sound.play(0,1);
            BGM3ON=true;
        }
    }
}

I hope that someone will help me, as I grew agitated (╯°□°)╯︵ ┻━┻ to just understand the problem here, and how to fix it, though, uncle Google didn't had any good answers for me; thus this post. Please, help me, thank you (シ_ _)シ</p>

4

1 回答 1

0

You might want to check out the SoundChannel reference. It has an example of how to make a button play and stop a sound.

From the code you posted, it looks like you are using SoundChannel wrong. You should be assigning the sound to the channel when you play it, and calling stop on the channel.

So something like:

bgmChannel = bgm1Sound.play(0,1);

Starts bgm1Sound playing in the sound channel.

When you want to stop the sound playing in the channel, just call bgmChannel.stop();. It will stop whatever sound is playing in that channel.

If you're only playing one sound at a time, then you should only need one sound channel, not three. Each time you start a new sound, assign it to your channel, and then call stop() on that channel to stop the sound.

于 2013-07-14T21:29:49.567 回答