flash as3 仅当定义了函数内的实例时才运行函数

发布于 2024-10-20 02:24:08 字数 943 浏览 1 评论 0原文

我遇到的情况是,在加载 swf 后,我向舞台添加一个事件侦听器以侦听键盘命令。其中之一是监听空格键,如果按下空格键,则应该播放电影。问题是,该电影直到稍后才会加载,具体取决于其他用户交互,因此,在加载之前,对其的引用将是未定义的。但是,如果我等待并仅在加载电影时添加键盘命令的侦听器,那么我无法将侦听器用于其他键盘命令,例如“向右”或“向左”,这些按钮首先让您进入电影。如果视频尚未定义,是否有办法添加条件或其他内容来防止执行该代码?这是我的代码:

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])-->
}

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])-->
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月棠 2024-10-27 02:24:08

要检查变量是否已定义,只需使用 if(typeof myVar == 'undefined')if(myVar === undefined)

要检查变量是否已定义但未分配值,请使用以下命令:

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

现在转到您的特定情况,在 loadVideo 函数内声明 video 变量/method,它永远不会在该范围之外可用。因此,您需要在更高级别上声明它才能使用它。

其他错误:
- 在 togglePause 中,参数应该是可选的,以允许手动调用函数
- 在 keyPressedDown 中,变量 key 未定义

尝试进行更改:

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])-->
}

免责声明:未经测试,但它可能会工作,或者至少接近工作.

问候,
阿林

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

如梦 2024-10-27 02:24:08

你可以在那里扔一个 try/catch 来处理它。不是超级高效,但在这种情况下它可能不会造成任何伤害:

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

或者,您可以定义一个布尔变量,例如:

var isLoaded:Boolean = false;

然后,有一个加载侦听器,当触发时,更改 isLoaded = true,然后:

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

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文