Search code examples
arraysactionscript-3nestedmovieclip

access a movie clip on a certain frame within an array as3


I have movie clip in an array (newStep) that is being added to the stage dynamically. It's randomly choosing a frame to go to every time an instance is added. There is a nested movie clip (stepLine) that i need to change the alpha of. This code actually works for adding a string to to the dynamic text box (pointsDText) but when i try to access the nested movie clip (stepLine) it gives me 1009 null object reference error. The funny thing is the code actually works and does change the alpha of the movie clip but i still get that error, and i think it's making my game more glitchy. I've tried using if(contains(steps[r].stepLine)) too but it doesn't work. Is there a better way to access this movie clip without getting the error?

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;
        }
    }
}

This is so difficult to explain but i hope you understand.

Thanks so much.


Solution

  • A null reference error happens when you try to access properties of a variable that doesn't point to any object -- a null reference. You're effectively trying to access an object that doesn't exist. For example, perhaps stepLine doesn't exist in one of those instances, so stepLine.alpha is causing the error. (How do you set the alpha of a non-existent clip?) Maybe the steps[r] clip is on a frame where there is not yet any stepLine MovieClip.

    You should run the movie in debug mode by pressing Ctrl+Shift+Enter in the Flash IDE. This should show you the exact line that causes the error, and it will let you inspect the values of any variables at that point. This should help you track down the problem. Similarly, you can use trace statements to aid in debugging. For example, you could trace(steps[r].stepLine); to check for null values, or even simply if(!steps[r].stepLine) trace("ERROR");. Additionally, if you wrap your accesses in if statements, you can avoid the null reference errors, even though this doesn't really address the underlying problem:

    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;
                }
            }
        }
    }