flash - Actionscript3 : Does variable exist?

标签 flash actionscript-3 variables adobe exists

我对 Actionscript 有点陌生,但我无法弄清楚这一点。我在这个话题上做了很多搜索,但没有找到明确的答案。我已经尝试了人们在网上发布的以下解决方案,但都没有奏效。

以下所有解决方案都给出了错误:1120: Access of undefined property myVariable

建议#1:

try {
     trace(myVariable); }
catch {
     trace("your variable doesn't exist"); }

建议#2:
if (myVariable) {
     trace("your variable exists!!"); }
else {
     trace("it doesn't exist"); }

建议#3:
if ( myVariable == null )
     trace("your variable doesn't exist");

建议#4:
if ( myVariable == undefined )
     trace("your variable doesn't exist");

就像我说的那样,我在网上找到了许多论坛帖子和内容,它们给出了上述建议,说它们会起作用,但它们似乎都给了我同样的 1120: Access of undefined property myVariable 错误。

顺便说一句,如果您想知道为什么我需要检查变量是否存在,我计划将变量传递给 SWF 中的 URL,因此我需要确保存在正确的变量并处理代码如果没有传入,则正确。

感谢您的快速回复。仍然没有真正起作用。变量的范围只是在脚本的顶层/根级别。基本上,我开始一个新的 Flash 文件,在第一帧我添加以下操作:
// to check for this.myVariable
if ( this.hasOwnProperty( "myVariable" ) ) {
     trace("myVariable exists");
}
else
{
     //Variable doesn't exist, so declare it now
     trace("declaring variable now...");
     var myVariable = "Default Value";
}

trace(myVariable);

当我运行 flash 文件时,我得到以下输出:
myVariable exists
undefined

我期待这个:
declaring variable now...
Default Value

最佳答案

By the way, in case you are wondering why I would need to check if a variable exists or not, I'm planning on passing variables to the SWF in its URL, so I need to make sure the proper variables exist and handle the code properly if they are not passed in.



那么你采取了错误的方法。下面是读取和验证 SWF 参数的正确方法™,以及如果它们不存在的默认值:
private function parameter(name:String, defaultValue:String):String
{
        // Get parameter list
    var paramObj:Object = LoaderInfo(stage.loaderInfo).parameters;

        // Check if parameter exists
    if(paramObj.hasOwnProperty(name) && paramObj[name] != "")
        return paramObj[name];                     
    else
        return defaultValue;
}

警告!由于这依赖于 'stage' 属性,请使用来自文档类或之后的代码 Event.ADDED_TO_STAGE .

关于flash - Actionscript3 : Does variable exist?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1482837/

相关文章:

flash - 优化 Actionscript 3 中的碰撞检测。

android - actionscript 3 - 播放声音一次然后延迟

apache-flex - 在类之间调度事件

python - 我正在更改一个变量,但是如何在函数结束时返回它的原始状态?

java - 重命名所有变量和方法名称的脚本或工具有什么建议吗?

flash - 无法通过 LAN 安装 Red5 演示 - 错误 : NetConnection. Connect.Failed

javascript - 打印时更改元素的颜色

apache-flex - Flex TextArea - 如何突出显示光标下的行/行?

php - 将 Img 源与 php 变量一起使用?

Flash:addEventListener 未检测到输入文本框中的粘贴事件