Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 访问阵列as3中特定帧上的电影剪辑_Arrays_Actionscript 3_Nested_Movieclip - Fatal编程技术网

Arrays 访问阵列as3中特定帧上的电影剪辑

Arrays 访问阵列as3中特定帧上的电影剪辑,arrays,actionscript-3,nested,movieclip,Arrays,Actionscript 3,Nested,Movieclip,我有一个数组newStep中的电影剪辑,它被动态地添加到舞台上。它是在每次添加实例时随机选择要转到的帧。有一个嵌套的电影剪辑步骤行,我需要更改其alpha。此代码实际上用于向动态文本框PointsText添加字符串,但当我尝试访问嵌套的电影剪辑stepLine时,它会给我1009 null对象引用错误。有趣的是,代码确实有效,并且确实改变了电影剪辑的alpha值,但我仍然得到了那个错误,我认为这让我的游戏更容易出错。我也尝试过使用ifcontainssteps[r].stepLine,但它不起作

我有一个数组newStep中的电影剪辑,它被动态地添加到舞台上。它是在每次添加实例时随机选择要转到的帧。有一个嵌套的电影剪辑步骤行,我需要更改其alpha。此代码实际上用于向动态文本框PointsText添加字符串,但当我尝试访问嵌套的电影剪辑stepLine时,它会给我1009 null对象引用错误。有趣的是,代码确实有效,并且确实改变了电影剪辑的alpha值,但我仍然得到了那个错误,我认为这让我的游戏更容易出错。我也尝试过使用ifcontainssteps[r].stepLine,但它不起作用。有没有更好的方法访问此电影剪辑而不出错

if(newStep != null){
    for(var r:int = 0; r<steps.length;r++){
        if(steps[r].currentLabel == "points"){
            steps[r].pointsDText.text = String(hPoints);
        }
        if(steps[r].currentLabel == "special"){
            steps[r].stepLine.alpha = sStepAlpha;   
        }
        if(steps[r].currentLabel == "life"){
            steps[r].stepLine.alpha = hStepAlpha;
        }
    }
}
这很难解释,但我希望你能理解


非常感谢。

当您尝试访问不指向任何对象的变量的属性时,会发生空引用错误—空引用。您实际上是在尝试访问一个不存在的对象。例如,其中一个实例中可能不存在stepLine,因此stepLine.alpha导致了错误。如何设置不存在剪辑的alpha?也许steps[r]剪辑在一个还没有任何stepLine MovieClip的框架上

您应该在Flash IDE中按Ctrl+Shift+Enter以调试模式运行电影。这将向您显示导致错误的确切行,并允许您在该点检查任何变量的值。这将有助于您追踪问题。类似地,您可以使用跟踪语句来帮助调试。例如,您可以跟踪ps[r].stepLine;检查空值,甚至只是如果!步骤[r]。步骤行跟踪错误;。此外,如果将访问封装在if语句中,则可以避免空引用错误,即使这并不能真正解决根本问题:

if(newStep != null){
    for(var r:int = 0; r<steps.length;r++){
        // only touch things if the movieclip actually exists
        if(steps[r] && steps[r].stepLine){
            if(steps[r].currentLabel == "points"){
                steps[r].pointsDText.text = String(hPoints);
            }
            if(steps[r].currentLabel == "special"){
                steps[r].stepLine.alpha = sStepAlpha;   
            }
            if(steps[r].currentLabel == "life"){
                steps[r].stepLine.alpha = hStepAlpha;
            }
        }
    }
}

这就解决了问题。非常感谢!