Search code examples
actionscript-3flashconditional-statementsmemory-efficient

Using a variable to allow only one video to play at a time


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.

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.

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).

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...

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");
    }
}

Solution

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