0

我最近創建了一個有四個按鈕的項目。每個按鈕都會將FLV播放器添加到舞臺並播放視頻。在測試過程中,我注意到在第一個功能完成加載電影之前點擊另一個按鈕,將播放兩部電影。使用變量一次只允許播放一個視頻

有沒有更有效的方法來處理這個問題?我覺得有很多的在這裏冗餘代碼,有沒有更好的方式來做到以下幾點:

  • 按鈕一按下
  • 加載視頻一
  • 忽略所有其它事件偵聽器,直到電影結束時,或用戶駁回視頻

我最初雖然創建一個兩個函數,一個刪除所有事件偵聽器,另一個添加所有事件偵聽器是一個好主意。按下按鈕時,在加載FLVPlayback之前,請刪除所有事件偵聽器。當電影完成時,將所有偵聽器添加回來。這看起來很累,而且效率很低。

我寫了下面的代碼作爲我想要做的一個例子。 (然而,每次輸出相同的字符串「One Can play」)。

var canLaunchOne:Boolean; 
var canLaunchTwo:Boolean; 
var canLaunchThree:Boolean; 
var canLaunchFour:Boolean; 

buttonMCOne.addEventListener(MouseEvent.MOUSE_DOWN, playMovieOne); 
buttonMCTwo.addEventListener(MouseEvent.MOUSE_DOWN, playMovieTwo); 
buttonMCThree.addEventListener(MouseEvent.MOUSE_DOWN, playMovieThree); 
buttonMCFour.addEventListener(MouseEvent.MOUSE_DOWN, playMovieFour); 

trace(canLaunchTwo); 

function playMovieOne(event:Event):void { 
    canLaunchOne = true; 
    canLaunchTwo = false; 
    canLaunchThree = false; 
    canLaunchFour = false; 
    trace(canLaunchOne); 
    playTheRightVideo(); 

} 

function playMovieTwo(event:Event):void { 
    canLaunchOne = false; 
    canLaunchTwo = true; 
    canLaunchThree = false; 
    canLaunchFour = false; 
    playTheRightVideo(); 

} 

function playMovieThree(event:Event):void { 
    canLaunchOne = false; 
    canLaunchTwo = false; 
    canLaunchThree = true; 
    canLaunchFour = false; 
    playTheRightVideo(); 

} 

function playMovieFour(event:Event):void { 
    canLaunchOne = false; 
    canLaunchTwo = false; 
    canLaunchThree = false; 
    canLaunchFour = true; 
    playTheRightVideo(); 

} 

function playTheRightVideo():void { 
    if(canLaunchOne = true){ 
     trace("One Can Play"); // do all that video stuff for video one 
    } else if(canLaunchTwo = true){ 
     trace("Two Can Play"); // do all that video stuff for video one 
    } else if(canLaunchThree = true){ 
     trace("Three Can Play"); // do all that video stuff for video one 
    } else { 
     trace("Four Can Play"); 
    } 
} 

我嘗試這個代碼,我得到了它的「工作」,但每個功能將始終設置canLaunchMovie爲true ...

import flash.events.Event; 

var canLaunchMovie:Boolean; 

buttonMCOne.addEventListener(MouseEvent.MOUSE_DOWN, playMovieOne); 
buttonMCTwo.addEventListener(MouseEvent.MOUSE_DOWN, playMovieTwo); 
buttonMCThree.addEventListener(MouseEvent.MOUSE_DOWN, playMovieThree); 
buttonMCFour.addEventListener(MouseEvent.MOUSE_DOWN, playMovieFour); 

function playMovieOne(e:Event):void { 
    if(canLaunchMovie = true){ 
     this.canLaunchMovie = false; 
     trace("One Can Play"); // do all that video stuff for video one 
    } else { 
     trace("I can't do that(play movie one) Dave"); 
    } 
} 

function playMovieTwo(e:Event):void { 
    if(canLaunchMovie = true){ 
     this.canLaunchMovie = false; 
     trace("Two Can Play"); // do all that video stuff for video one 
    } else { 
     trace("I can't do that(play movie two) Dave"); 
    } 
} 

function playMovieThree(e:Event):void { 
    if(canLaunchMovie = true){ 
     this.canLaunchMovie = false; 
     trace("Three Can Play"); // do all that video stuff for video one 
    } else { 
     trace("I can't do that(play movie three) Dave"); 
    } 
} 

function playMovieFour(e:Event):void { 
    if(canLaunchMovie = true){ 
     this.canLaunchMovie = false; 
     trace("Four Can Play"); // do all that video stuff for video one 
    } else { 
     trace("I can't do that(play movie four) Dave"); 
    } 
} 
+0

精氨酸。因此,技術更新不起作用。我正在做一個永遠是真的任務,而不是做平等檢查。它可以工作,因爲每個按鈕都會分配canLaunchMovie = true; – elCavador

+0

與論壇網站不同,我們不使用「謝謝」,或「任何幫助表示讚賞」,或在[so]上簽名。請參閱「[應該'嗨','謝謝',標語和致敬從帖子中刪除?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be - 從帖子中刪除)。順便說一句,它是「提前致謝」,而不是「感謝先進」。 –

回答

0

你並不需要有一個以上的FLVPlayback 。 您只需更改flvplayback的source即可。