flash as3 仅当定义了函数内的实例时才运行函数
我遇到的情况是,在加载 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要检查变量是否已定义,只需使用
if(typeof myVar == 'undefined')
或if(myVar === undefined)
。要检查变量是否已定义但未分配值,请使用以下命令:
现在转到您的特定情况,在
loadVideo
函数内声明video
变量/method,它永远不会在该范围之外可用。因此,您需要在更高级别上声明它才能使用它。其他错误:
- 在
togglePause
中,参数应该是可选的,以允许手动调用函数- 在
keyPressedDown
中,变量key
未定义尝试进行更改:
免责声明:未经测试,但它可能会工作,或者至少接近工作.
问候,
阿林
To check if a variable was defined or not just use
if(typeof myVar == 'undefined')
orif(myVar === undefined)
.To check if a variable was defined but wasn't assigned a value use the following:
Now going to your particular case, having that the
video
variable is declared inside theloadVideo
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 variablekey
is not definedTry this for a change:
Disclaimer: Not tested, but it will probably work, or at least be close to working.
Regards,
Alin
你可以在那里扔一个 try/catch 来处理它。不是超级高效,但在这种情况下它可能不会造成任何伤害:
或者,您可以定义一个布尔变量,例如:
然后,有一个加载侦听器,当触发时,更改 isLoaded = true,然后:
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:
Or, you could just define a boolean variable, like:
then, have a load listener, and when that fires, change isLoaded = true, then: