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

时间:2015-05-06 04:51:29

标签: actionscript-3 flash conditional-statements memory-efficient

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

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

  • Button One Pressed
  • 加载视频一
  • 忽略所有其他事件监听器,直到电影结束,或者用户取消视频(点击要关闭)

我最初虽然创建两个函数是一个好主意,一个删除所有事件侦听器,另一个添加所有事件侦听器。按下按钮时,在加载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");
    }
}

我尝试了这段代码,我得到了#34;工作",但每个函数都会将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 个答案:

答案 0 :(得分:0)

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