【问题标题】:access a movie clip on a certain frame within an array as3访问数组 as3 中某个帧上的影片剪辑
【发布时间】:2011-04-03 13:26:02
【问题描述】:

我在一个动态添加到舞台的数组 (newStep) 中有影片剪辑。每次添加实例时,它都会随机选择要转到的帧。有一个嵌套的影片剪辑(stepLine),我需要更改它的 alpha。此代码实际上适用于将字符串添加到动态文本框(pointsDText),但是当我尝试访问嵌套影片剪辑(stepLine)时,它给了我 1009 空对象引用错误。有趣的是代码实际上可以工作并且确实改变了电影剪辑的 alpha,但我仍然得到那个错误,我认为它让我的游戏更加故障。我也尝试过使用 if(contains(steps[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;
        }
    }
}

这很难解释,但我希望你能理解。

非常感谢。

【问题讨论】:

    标签: arrays actionscript-3 nested movieclip


    【解决方案1】:

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

    您应该在 Flash IDE 中按 Ctrl+Shift+Enter 以调试模式运行影片。这应该向您显示导致错误的确切行,并且它将让您检查该点的任何变量的值。这应该可以帮助您找出问题所在。同样,您可以使用跟踪语句来帮助调试。例如,您可以trace(steps[r].stepLine); 来检查空值,甚至可以简单地使用if(!steps[r].stepLine) trace("ERROR");。此外,如果您将访问包装在 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;
                }
            }
        }
    }
    

    【讨论】:

    • 修复了它。非常感谢!
    猜你喜欢
    • 2012-05-24
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    相关资源
    最近更新 更多