2010-11-12 69 views
0

我有這個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; 
     } 
    } 
} 

回答

4

我強烈建議不要採取某種形式的標誌或計時器破解來解決不明原因的行爲 - 這會導致代碼膨脹並導致線路長期運行,從而導致更多問題。

理解它爲什麼要調用你的函數兩次要好得多。投資時間來弄清楚(你是否有聽衆設置在多個顯示對象上?這是否是一種冒泡的問題?它可能是你的鼠標嗎?試試一個乾淨的Flash項目只需一個處理程序,看看是否調用兩次,等等) ,不僅您的代碼會更好更快,而且您還將在此過程中學到很多東西。

+1

我完全同意這一點。 – adamzwakk 2010-11-12 15:35:01

+0

我試過了。檢查並重新檢查,但我仍然不知道它是如何設法運行兩次。唯一可能的是我可以想到導致問題的原因可能是這2個自定義類(包括這1個)可以相互通信。但即使當我像其他自定義類一樣使用像exp:var me:MEfunction = new MEfunction()時,但我有第一個公用函數MEfunction(whis是mouseEvent按鈕的位置)留空。所以它不應該直接從ME功能返回任何東西。 – Hwang 2010-11-12 21:41:12

+0

你能否更新你的問題以包含代碼?它很長嗎?更容易診斷。 – Myk 2010-11-12 21:47:20

0

您可以創建一個變量,使其只運行一次。像...

var runOnce:Boolean = false; 

button.addEventListener(MouseEvent.CLICK, testEvent); 

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

這有點懶,但你明白了。