Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4026384
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:57:49+00:00 2026-05-20T10:57:49+00:00

I have a situation where, upon loading my swf, I add an event listener

  • 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])-->
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:57:50+00:00Added an answer on May 20, 2026 at 10:57 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the situation: I have been called upon to work with InstallAnywhere 8,
I have a situation where I want to copy the output assembly from one
I have stumbled upon a situation where I cannot start more than a certain
We have a situation where users are allowed to upload content, and then separately
We have a situation in our product where for a long time some data
I have a situation where I might have multiple instances of a program running
I have the situation where i use GIS software which stores the information about
I have a situation where I want to create a signature of a data
I have a situation where I have two entities that share a primary key
I have a situation where another developer is including source files from a project

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.