Actionscript 3.0方法不断重复,无法弄清楚原因

时间:2013-07-26 14:25:46

标签: actionscript-3 methods actionscript

调试音频滑块时遇到问题。我很确定我的问题在于我的一种方法changeVolumeRedFireball只是以非常快的速度不断重复。我在游戏中偶尔会听到一声毛刺的声音,所以它似乎有关联。我在方法中追踪“输出”并很快发现它正以高速率重复。

问题是,我无法弄清楚这是来自哪里!另一个说明。只有在按住滑块后才开始重复,因此从changeVolumeRedFireball

激活dragSliderRedFireball

我确实有其他类的其他方法引用此类中的方法。他们只访问playSoundRedFireballstopSoundRedFireball,所以我不明白为什么会产生任何影响。此外,这个类在我启动游戏时由我的文档类实例化。我想如果要求我会从文档类中输入相关代码,但我认为它根本不会影响这个问题。

package {

        import flash.display.Sprite;
        import flash.display.Graphics;
        import flash.events.MouseEvent;
        import flash.events.Event;
        import flash.net.URLRequest;
        import flash.media.Sound;
        import flash.media.SoundChannel;
        import flash.media.SoundTransform;
        import flash.geom.Rectangle;

        public class VolumeRedFireball extends Sprite {

                public var redFireballSnd:Sound = new Sound();
                public var redFireballChannel:SoundChannel = new SoundChannel();
                //URLRequest=new URLRequest("solitude.wav");
                //Make sure you pass URLRequest an audio file on your computer.
                public var reqRedFireball:EnemyAppearSound = new EnemyAppearSound();
                public var boundaryRedFireball:Rectangle;
                public var spriteRedFireball:Sprite;
                public var sliderRedFireball:Sprite;
                public var xPosRedFireball:Number;
                public var yPosRedFireball:Number;
                public static var volRedFireball:Number = 1;

                public function VolumeRedFireball() {
                        this.addEventListener(Event.ADDED_TO_STAGE, onStageRedFireball,false,0,true);
                        volRedFireball=1;
                        redFireballChannel.soundTransform=new SoundTransform(volRedFireball)
                }

                public function onStageRedFireball(e:Event):void
                {
                    //We remove it immediately so that it doesn't get called multiple times
                    //As the instance is added to the display list tree
                    this.removeEventListener(Event.ADDED_TO_STAGE, onStageRedFireball);

                    xPosRedFireball = 320;
                    yPosRedFireball = 170;

                    initRedFireball();
                }


                public function initRedFireball():void {
                        spriteRedFireball = new Sprite();
                        redFireballChannel.stop();
                        spriteRedFireball.graphics.beginFill(0x999999);
                        spriteRedFireball.graphics.drawRect(xPosRedFireball,yPosRedFireball,100,5);
                        spriteRedFireball.graphics.endFill();
                        addChild(spriteRedFireball);
                        spriteRedFireball.x-=spriteRedFireball.width/2;
                        sliderRedFireball = new Sprite();
                        sliderRedFireball.graphics.beginFill(0xFF0000);
                        sliderRedFireball.graphics.drawCircle(xPosRedFireball+50,yPosRedFireball, 15);
                        sliderRedFireball.graphics.endFill();
                        addChild(sliderRedFireball);
                        sliderRedFireball.addEventListener(MouseEvent.MOUSE_DOWN, dragsliderRedFireball);
                        stage.addEventListener(MouseEvent.MOUSE_UP, stopsliderRedFireball);
                        boundaryRedFireball=new Rectangle(-100,0,100,0);
                }


                public function dragsliderRedFireball(event:MouseEvent):void {
                        sliderRedFireball.startDrag(false,boundaryRedFireball);
                        sliderRedFireball.removeEventListener(MouseEvent.CLICK, dragsliderRedFireball);
                        sliderRedFireball.addEventListener(Event.ENTER_FRAME, changeVolumeRedFireball);
                }


                public function stopsliderRedFireball(event:MouseEvent):void {
                        sliderRedFireball.stopDrag();
                        sliderRedFireball.removeEventListener(MouseEvent.MOUSE_UP, stopsliderRedFireball);
                }



                public function changeVolumeRedFireball(event:Event):void {
                        volRedFireball=1+Math.round(sliderRedFireball.x)/100;
                        redFireballChannel.soundTransform=new SoundTransform(volRedFireball);
                        trace("output");   
                }


                public function playSoundRedFireball():void
                {
                    redFireballChannel = reqRedFireball.play();
                }

                public function stopSoundRedFireball():void
                {
                    redFireballChannel.stop();
                }

        }

}

1 个答案:

答案 0 :(得分:2)

在每一帧中更改SoundTransform并不好,因为您实际上正在破坏音频通道。如果您使用MouseEvent.MOUSE_MOVE来触发音量更改,就好像移动鼠标并拖动音量滑块一样,那么SWF用户显然希望更改音量。但是,如果用户开始拖动滑块但不移动它,为什么要更改音量?

public function dragsliderRedFireball(event:MouseEvent):void {
                    sliderRedFireball.startDrag(false,boundaryRedFireball);
                    sliderRedFireball.removeEventListener(MouseEvent.MOUSE_DOWN, dragsliderRedFireball);
                    sliderRedFireball.addEventListener(MouseEvent.MOUSE_MOVE, changeVolumeRedFireball);
                    sliderRedFireball.removeEventListener(MouseEvent.MOUSE_UP, stopsliderRedFireball);
            }


            public function stopsliderRedFireball(event:MouseEvent):void {
                    sliderRedFireball.stopDrag();
                    sliderRedFireball.removeEventListener(MouseEvent.MOUSE_UP, stopsliderRedFireball);
                    sliderRedFireball.removeEventListener(MouseEvent.MOUSE_MOVE, changeVolumeRedFireball);
                    sliderRedFireball.addEventListener(MouseEvent.MOUSE_DOWN, dragsliderRedFireball);
            }

另外,你搞砸了你的听众。首先,在停止拖动火球后,您不会删除enterframe侦听器。其次,在释放火球后,您不会再添加启动拖动侦听器。第三,在您的initRedFireball中,您正在添加stopsliderRedFireball作为阶段的聆听者,原因很奇怪,但您尝试将其从sliderRedFireball中删除。请准确注意听众去哪里,他们听什么,你把它们从哪里删除,以及从哪些对象中删除。滥用enterframe监听器可以很快建立起来,并且破坏你所有的乐趣。