使用变量一次只能播放一个视频

时间:2022-07-23 19:35:53

I recently created a project that has four buttons. Each button adds an FLV player to the stage and plays a video. During testing, I noticed that clicking another button before the first function is finished loading the movie, TWO movies will play.

我最近创建了一个有四个按钮的项目。每个按钮都会在舞台上添加一个FLV播放器并播放视频。在测试期间,我注意到在第一个功能完成加载电影之前点击另一个按钮,将播放两部电影。

Is there a more efficient way of handling this? I feel like there is a lot of redundant code here, is there a better way to do the following:

有没有更有效的方法来处理这个?我觉得这里有很多冗余代码,有没有更好的方法来执行以下操作:

  • Button One Pressed
  • 按钮一按

  • Load Video One
  • 加载视频一

  • Ignore all other event listeners until the movie ends, or the user dismisses the video (Clicks to dismiss)
  • 忽略所有其他事件侦听器,直到电影结束,或者用户解除视频(点击要关闭)

I initially though it would be a good idea to create a two functions, one that removes all event listeners, and another that adds all the event listeners. When a button is pressed, before it loads the FLVPlayback, remove all event listeners. When the movie is COMPLETE, add all the listeners back. That seems taxing, and very inefficient.

我最初虽然创建两个函数是一个好主意,一个删除所有事件侦听器,另一个添加所有事件侦听器。按下按钮时,在加载FLVPlayback之前,请删除所有事件侦听器。当电影完成时,将所有侦听器添加回来。这似乎很费劲,效率很低。

I wrote the following code as an example of what I'm trying to do. (This outputs the same string "One Can play" every time though).

我写了下面的代码作为我正在尝试做的一个例子。 (每次输出相同的字符串“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");
    }
}

I tried this code, and I got it "working", but every function will always set canLaunchMovie to true...

我尝试了这个代码,我让它“正常工作”,但每个函数都会将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");
    }
}

1 个解决方案

#1


You don't need to have more than one flvplayback. You only need to change the source of your flvplayback.

您不需要有多个flvplayback。您只需要更改flvplayback的来源。

#1


You don't need to have more than one flvplayback. You only need to change the source of your flvplayback.

您不需要有多个flvplayback。您只需要更改flvplayback的来源。