使用变量一次只允许播放一个视频
Using a variable to allow only one video to play at a time
我最近创建了一个有四个按钮的项目。每个按钮都会在舞台上添加一个 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");
}
}
我试过这段代码,我明白了 "working",但是每个函数总是将 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");
}
}
您不需要超过一个 flvplayback。
您只需要更改您的 flvplayback 的 source
。
我最近创建了一个有四个按钮的项目。每个按钮都会在舞台上添加一个 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");
}
}
我试过这段代码,我明白了 "working",但是每个函数总是将 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");
}
}
您不需要超过一个 flvplayback。
您只需要更改您的 flvplayback 的 source
。