0

I have a situation where, upon loading my swf, I add an event listener to the stage to listen for keyboard commands. One of them is to listen for the spacebar, and if it is pressed, it should play a movie. The problem is, that movie is not loaded until later on, depending other user interactions, therefore, until it is loaded, the reference to it would be undefined. But if I wait and add the listener for key commands only when the movie is loaded, then I can't utilize the listener for other keyboard commands, like RIGHT or LEFT, which are the buttons which get you to the movie in the first place. Is there a way to add a conditional or something to prevent that code from being executed if the video is not defined yet? Here's my code:

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);

function togglePause(event:MouseEvent):void {
     video.videoPaused = !video.videoPaused; //video is undefined initially so this throws errors.
}

function keyPressedDown(event:KeyboardEvent):void {
     switch (key) {
        case 32: //Spacebar
        //togglePause();
     }
}

function loadVideo(){ //called based on user interactions which happen later on
    var video:VideoLoader = new VideoLoader(project_array[cp].project_display_files[0], {name:"myVideo", container:this, bgColor:0x000000, autoPlay:false, volume:1,onComplete:vidLoaded});
    video.load(); //currently set up for only one video ([0])-->
}

2 Answers 2

1

To check if a variable was defined or not just use if(typeof myVar == 'undefined') or if(myVar === undefined).

To check if a variable was defined but wasn't assigned a value use the following:

var myVar:MyClass;
if(myVar === null)
    ...

Now going to your particular case, having that the video variable is declared inside the loadVideo function/method, it will never be available outside that scope. So you need to declare it at a higher level to be able to work with it.

Other mistakes:
- in togglePause the parameter should be optional to allow for manual calling of the function
- in keyPressedDown the variable key is not defined

Try this for a change:

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);

var video:VideoLoader;

function togglePause(event:MouseEvent = null):void {
    if(video)
        video.videoPaused = !video.videoPaused; //video is undefined initially so this throws errors.
}

function keyPressedDown(event:KeyboardEvent):void {
     switch (event.charCode) {
        case 32: //Spacebar
            togglePause();
            break; // not necesary here, but good practice to add it!
     }
}

function loadVideo(){ //called based on user interactions which happen later on
    video = new VideoLoader(project_array[cp].project_display_files[0], {name:"myVideo", container:this, bgColor:0x000000, autoPlay:false, volume:1,onComplete:vidLoaded});
    video.load(); //currently set up for only one video ([0])-->
}

Disclaimer: Not tested, but it will probably work, or at least be close to working.

Regards,
Alin

Sign up to request clarification or add additional context in comments.

1 Comment

Ah - that's what I was doing wrong - I was declaring the video earlier, but not specifying it as type :VideoLoader, and referencing it later as video:VideoLoader. Once I reversed these two things, it worked. Thanks!
0

You could throw a try/catch in there to take care of it. Not super efficient, but in this case it wouldn't probably cause any harm:

try {
 switch (key) {
    case 32: //Spacebar
    //togglePause();
 }
} catch (e:Error){
    // trace(e.message) // if it's needed, otherwise just fail silently
}

Or, you could just define a boolean variable, like:

var isLoaded:Boolean = false;

then, have a load listener, and when that fires, change isLoaded = true, then:

switch (key) {
    case 32: //Spacebar
    if (isLoaded){
      //togglePause();
    }
}

2 Comments

Thanks - the problem was scope, as mentioned above, but the catch / try is a helpful technique to know.
yeah, works wonders in a pinch. not so optimized though if you start putting it into things that are called a lot over and over (ie: on a timer, or enter_frame) cause the catch has to figure out what it's catching and accomodate for that, even if it's commented out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.