停止在一段时间内运行多次的函数

时间:2010-11-12 15:23:03

标签: actionscript-3 mouseevent

我有这个MouseEvent函数,我完全不知道为什么它会被解雇两次。有没有办法可以在一段时间内禁用该功能?我尝试禁用按钮,但似乎直接调用了该功能,并且不会从按钮触发。

添加信息:当我向数组中添加更多对象时,该函数会触发更多时间

处理按钮的类

 package classes
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Rectangle;

    import classes.playVideo;
    import classes.playList;
    import classes.viewType;

    public class controlMenu extends MovieClip
    {
        private var playV:playVideo=new playVideo();
        private var list:playList=new playList();
        private var viewT:viewType = new viewType();

        private static var con:controls = new controls();

        private static var _buttonStatus:Boolean;

        public function controlMenu()
        {
        }

        //-------------------------------------------------------------

        public function loadControlMenu():void
        {
            this.addEventListener(Event.ADDED_TO_STAGE,add2Stage);
        }

        private function add2Stage(e:Event):void
        {
            if (stage.numChildren == 1)
            {
                con.x=(stage.stageWidth-230)/2;
                con.y=stage.stageHeight-(con.height+9);
                addChild(con);

                playButtonStatus();

                con.soundBtn.addEventListener(MouseEvent.MOUSE_OVER, soundOver);
                con.soundBtn.addEventListener(MouseEvent.MOUSE_OUT, soundOut);
                con.soundBtn.addEventListener(MouseEvent.MOUSE_DOWN, soundDown);
                stage.addEventListener(MouseEvent.MOUSE_UP, soundUp);

                con.prev.addEventListener(MouseEvent.CLICK,prevClick);
                con.next.addEventListener(MouseEvent.CLICK,nextClick);
            }
        }

        private function playClick(e:MouseEvent):void
        {
            if (e.currentTarget.currentFrameLabel == "play" && playV.currentVideoStatus == "play")
            {
                e.currentTarget.gotoAndStop("pause");
                playV.pauseStatus();
            }
            else if (e.currentTarget.currentFrameLabel=="pause" && playV.currentVideoStatus == "pause")
            {
                e.currentTarget.gotoAndStop("play");
                playV.playStatus();
            }
            else if (e.currentTarget.currentFrameLabel == "play"&& playV.currentVideoStatus == "end")
            {
                playV.newVideo = "video/video" + list.currentIndex + ".flv";
            }
        }

        private function playOver(e:MouseEvent):void
        {
            e.currentTarget.btn.gotoAndStop("over");
        }

        private function playOut(e:MouseEvent):void
        {
            e.currentTarget.btn.gotoAndStop("out");
        }

        //-------------------------------------------------------------

        private function soundOver(e:MouseEvent):void
        {
            e.currentTarget.gotoAndStop("over");
        }

        private function soundOut(e:MouseEvent):void
        {
            if (e.buttonDown == false)
            {
                e.currentTarget.gotoAndStop("out");
            }
        }

        private function soundDown(e:MouseEvent):void
        {
            var volumeBound:Rectangle = new Rectangle(-93,35,80,0);
            e.currentTarget.startDrag(false, volumeBound);

            stage.addEventListener(MouseEvent.MOUSE_MOVE,soundAdjust);
        }

        private function soundUp(e:MouseEvent):void
        {
            con.soundBtn.stopDrag();
            con.soundBtn.gotoAndStop("out");

            stage.removeEventListener(MouseEvent.MOUSE_MOVE,soundAdjust);
        }

        private function soundAdjust(e:MouseEvent):void
        {
            playV.adjustSound = ((con.soundBtn.x+93)*100)/80;
        }

        public function set adjustSoundBtnPos(a:Number):void
        {
            var newPos:Number = ((80*a)/100);
            con.soundBtn.x = newPos - 93;
        }

        private function prevClick(e:MouseEvent):void
        {
            if (viewT.viewIn == "stream" && list.currentIndex > 1)
            {
                var goBack = list.currentIndex - 1;
                list.currentIndex = goBack;
                playV.newVideo = "video/video" + goBack + ".flv";
                list.currentVideoLink = goBack;
            }
        }

        private function nextClick(e:MouseEvent):void
        {
            if (viewT.viewIn == "stream" && list.currentIndex < list.vDataLength)
            {
                var goNext = list.currentIndex + 1;
                list.currentIndex = goNext;
                playV.newVideo = "video/video" + goNext + ".flv";
                list.currentVideoLink = goNext;
            }
        }

        //-------------------------------------------------------------

        public function get currentStatus():String
        {
            return con.playBtn.currentFrameLabel;
        }

        public function resetPlayStatus():void
        {
            con.playBtn.gotoAndStop("play");
        }

        //-------------------------------------------------------------

        public function set buttonStatus(s:Boolean):void
        {
            _buttonStatus = s;
            playButtonStatus();
        }

        public function playButtonStatus():void
        {
            if (_buttonStatus == true)
            {
                con.playBtn.buttonMode = true;
                con.playBtn.addEventListener(MouseEvent.CLICK, playClick);
                con.playBtn.addEventListener(MouseEvent.MOUSE_OVER, playOver);
                con.playBtn.addEventListener(MouseEvent.MOUSE_OUT, playOut);

                con.soundBtn.buttonMode = true;
                con.soundBtn.mouseEnabled = true;
            }
            else
            {
                con.playBtn.buttonMode = false;
                con.playBtn.removeEventListener(MouseEvent.CLICK, playClick);
                con.playBtn.removeEventListener(MouseEvent.MOUSE_OVER, playOver);
                con.playBtn.removeEventListener(MouseEvent.MOUSE_OUT, playOut);

                con.soundBtn.buttonMode = false;
                con.soundBtn.mouseEnabled = false;
            }
        }
    }
}

导致mouseevent函数的额外循环的类

 package classes
{
    import flash.media.Video;
    import flash.display.MovieClip;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.events.AsyncErrorEvent;
    import flash.events.TimerEvent;
    import flash.events.Event;
    import flash.utils.Timer;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.media.SoundTransform;
    import flash.media.SoundMixer;
    import flash.net.URLLoader;
    import flash.net.URLRequest;


    import classes.Playing;
    import classes.progressBar;
    import classes.controlMenu;
    import classes.viewType;
    import classes.downloadVideo;
    import classes.playList;
    import classes.playVideo;

    public class playVideo extends MovieClip
    {
        private var Play:Playing = new Playing();
        private var progressB:progressBar = new progressBar();
        private var conM:controlMenu;
        private var viewT:viewType=new viewType();
        private var downloadV:downloadVideo;
        private var list:playList;
        private var vBox:MovieClip = new MovieClip();

        private var nc:NetConnection;
        private static var ns:NetStream;
        private const buffer:Number = 2;
        private static var videoURL:String;
        private static var vid:Video = new Video();

        private static var meta:Object = new Object();
        private var durationSecs:Number;
        private var durationMins:Number;
        private var durSecsDisplay:String;
        private var durMinsDisplay:String;
        private static var videoDuration:String;

        private static var t:Timer = new Timer(100);

        private static var videoSound:SoundTransform = new SoundTransform();

        private var arial:Arial = new Arial();
        private static var durationTxt:TextField=new TextField();
        private var durationTxtF:TextFormat=new TextFormat();

        private var scrubForward:Boolean;
        private static var currentStatus;

        private static var nsArray:Array= new Array();
        private var dummyArray:Array = new Array();


        //--------------------------------------------------------------------------

        private var videoLoader:URLLoader;

        public function playVideo()
        {
        }

        public function set newVideo(v:String):void
        {
            currentStatus = "play";
            videoURL = v;

            if (viewT.viewIn == "stream")
            {
                addVideo();
            }
            else if (viewT.viewIn=="download")
            {
                for (var i:uint=0; i<nsArray.length; i++)
                {
                    if (nsArray[i] != null)
                    {
                        nsArray[i].close();
                    }
                }
                videoProperties();
                soundF();
            }
            conM.resetPlayStatus();
        }

        public function videoFunction():void
        {
            vBox.graphics.beginFill(0x000000);
            vBox.graphics.drawRect(0,36,668,410);
            vBox.graphics.endFill();
            addChild(vBox);

            // Add the instance of vid to the stage
            vid.width = vid.width * 2;
            vid.height = vid.height * 1.5;
            vid.x = vBox.width / 2 - vid.width / 2;
            vid.y = vBox.height / 2 - vid.height / 2 + 36;
            vBox.addChild(vid);

            if (viewT.viewIn == "stream")
            {
                videoProperties();
            }

            soundF();
            durationText();
        }

        public function videoProperties():void
        {
            nc=new NetConnection();

            // Assign variable name for Net Connection 
            nc.connect(null);

            // Assign Var for the NetStream Object using NetConnection
            ns = new NetStream(nc);

            // Add the buffer time to the video Net Stream
            ns.bufferTime = buffer;

            // Set client for Meta Data Function
            ns.client = {};
            ns.client.onMetaData = onMetaData;

            // Attach netStream to our new Video Object
            if (viewT.viewIn == "stream")
            {
                vid.attachNetStream(ns);
            }
            else if (viewT.viewIn == "download")
            {
                nsArray[list.currentIndex - 1] = ns;
                dummyArray.push(ns);
                vid.attachNetStream(ns);
            }

            addVideo();
        }

        private function addVideo():void
        {
            // Assign NetStream to start play automatically by default
            if (viewT.viewIn == "stream" && videoURL == null)
            {
                videoURL = "video/video1.flv";
            }

            ns.play(videoURL);

            conM= new controlMenu();
            conM.buttonStatus = true;
            progressB.progressBarStatus();

            // Add Error listener and listeners for all of our buttons
            ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR,asyncErrorHandler);

            currentStatus = "play";

            t.addEventListener(TimerEvent.TIMER,onTick);
            t.start();
        }

        private function asyncErrorHandler(event:AsyncErrorEvent):void
        {
            // Do whatever you want if an error arises
        }

        private function onMetaData(info:Object):void
        {
            meta = info;
            /*for (meta in info)
            {
            trace(meta + ":" + info[meta]);
            }*/

            durationSecs = Math.floor(meta.duration);
            durationMins = Math.floor(durationSecs / 60);
            durationMins %=  60;
            durationSecs %=  60;

            if (durationMins < 10)
            {
                durMinsDisplay = "0" + durationMins;
            }
            else
            {
                durMinsDisplay = "" + durationMins;
            }

            if (durationSecs < 10)
            {
                durSecsDisplay = "0" + durationSecs;
            }
            else
            {
                durSecsDisplay = "" + durationSecs;
            }

            videoDuration = durMinsDisplay + ":" + durSecsDisplay;
            Play.vDuration = videoDuration;
        }

        //--------------------------------------------------------------------------

        public function prepareArray():void
        {
            list = new playList();

            for (var i:uint=0; i<list.vDataLength; i++)
            {
                nsArray.push(null);
            }
        }

        //--------------------------------------------------------------------------

        private function onTick(event:TimerEvent):void
        {
            if (viewT.viewIn == "stream" || ns.bytesLoaded == ns.bytesTotal)
            {
                var nsSecs:Number = Math.floor(ns.time);
                var nsMins:Number = Math.floor(nsSecs / 60);

                nsMins %=  60;
                nsSecs %=  60;

                var nsSecsDisplay:String = "";
                var nsMinsDisplay:String = "";
                if (nsMins < 10)
                {
                    nsMinsDisplay = "0" + nsMins;
                }
                else
                {
                    nsMinsDisplay = "" + nsMins;
                }
                if (nsSecs < 10)
                {
                    nsSecsDisplay = "0" + nsSecs;
                }
                else
                {
                    nsSecsDisplay = "" + nsSecs;
                }

                durationTxt.text = nsMinsDisplay + ":" + nsSecsDisplay;
                progressB.progressing = ns.time / meta.duration * 668;
            }
            else if (viewT.viewIn=="download" && ns.bytesLoaded!=ns.bytesTotal)
            {
                list.preloadFlv();
            }

            if (durationTxt.text == videoDuration)
            {
                currentStatus = "end";
                t.stop();
                t.removeEventListener(TimerEvent.TIMER,onTick);
                ns.pause();
                ns.seek(0);
            }
        }

        //--------------------------------------------------------------------------

        private function soundF():void
        {
            var soundAmount:Number = 7;
            if (viewT.viewIn == "stream")
            {
                ns.soundTransform = videoSound;
                videoSound.volume = soundAmount / 10;
                conM.adjustSoundBtnPos = soundAmount * 10;
            }
            else if (viewT.viewIn=="download")
            {
                if (dummyArray.length == 1 && ns != null)
                {
                    ns.soundTransform = videoSound;
                    videoSound.volume = soundAmount / 10;
                    conM.adjustSoundBtnPos = soundAmount * 10;
                }
                else if (dummyArray.length> 1 && ns!=null)
                {
                    ns.soundTransform = videoSound;
                }
            }
        }

        public function set adjustSound(s:Number):void
        {
            videoSound.volume = s / 100;
            ns.soundTransform = videoSound;
        }

        //--------------------------------------------------------------------------

        private function durationText():void
        {
            durationTxtF = new TextFormat();
            durationTxtF.size = 12;
            durationTxtF.leftMargin = 5;
            durationTxtF.font = arial.fontName;

            durationTxt.defaultTextFormat = durationTxtF;
            durationTxt.selectable = false;
            durationTxt.textColor = 0x999999;
            durationTxt.width = 50;
            durationTxt.height = 22;
            durationTxt.x = 476;
            durationTxt.y = 477;
            durationTxt.text = "00:00";
            addChild(durationTxt);
        }

        //--------------------------------------------------------------------------

        public function get Duration():String
        {
            return videoDuration;
        }

        public function playStatus():void
        {
            conM=new controlMenu();
            if (conM.currentStatus == "play")
            {
                ns.resume();
                updateProgress();
            }

            if (currentStatus == "pause")
            {
                currentStatus = "play";
                t.start();
            }
        }

        public function pauseStatus():void
        {
            ns.pause();
            t.stop();
            trace("pause");
            if (currentStatus == "play")
            {
                currentStatus = "pause";
            }
        }

        public function get currentVideoStatus():String
        {
            return currentStatus;
        }

        public function restartVideo():void
        {
            ns.resume();
        }

        //--------------------------------------------------------------------------

        public function updateProgress():void
        {
            ns.seek((progressB.currentProgress / 668) *meta.duration);
        }

        public function get nsBytesTotal():Number
        {
            return ns.bytesTotal;
        }

        public function get nsBytesLoaded():Number
        {
            return ns.bytesLoaded;
        }

        public function get getVideoURL():String
        {
            return videoURL;
        }
    }
}

2 个答案:

答案 0 :(得分:4)

我强烈建议不要做某种标记或计时器黑客来解决一个无法解释的行为 - 这会导致代码膨胀,从长远来看会导致更多问题。

了解为什么它会两次调用你的函数要好得多。花时间搞清楚(你有没有设置多个显示对象的听众?这是一个冒泡的问题吗?它可能是你的鼠标?尝试一个干净的flash项目,只需一个处理程序,看看是否有两次调用,等等) ,并且您的代码不仅会更好更快,而且您在此过程中也会学到很多东西。

答案 1 :(得分:0)

您可以创建一个变量,因此它只运行一次。有点像...

var runOnce:Boolean = false;

button.addEventListener(MouseEvent.CLICK, testEvent);

function testEvent(e:Event):void{
if(runOnce == false){
//do stuff here
runOnce = true;
}

这有点懒,但你明白了。