3
votes

This is more a question about writing the code than a particular problem (although it is a particular problem). Note, I'm self taught so there is much I do not know in case this is a realtively simple issue:

I have a movie clip for which I've created a class. On my main timeline, I've instatiated (addChild) it within a function using a variable within that function, e.g.,:

function myfunction():void {
     var newInstance:MovieClip = new myCreatedClassForTheMovieClip();
     addChild(newInstance);
      ....
}

Within my movie clip, I reference a variable on the main timeline: movieClipVar = MovieClip(root).mainTimeLineVariable; I get error Error #1009: Cannot access a property or method of a null object reference.

When I make the variable declaration for the new instance of the movie clip outside of the function but at the global level, I don't get that error BUT, when I try to removeChild(newInstance) I get compiler error 1120 : access of undefined property newInstance (which does make sense since it isn't instantiated yet).

So, I'm not sure how the two objects are working together (the instantiated movie clip and main timeline) and why the movie clip can't see the varaible on the timeline even with MovieClip(root) to point it there.

Thanks for any help or guidance on this.

Cheers,

Mike

edit: When I declare the newInstance globally, I instantiate it the same way in the function, just omitting the var statement and using the addChild(newInstance).

Here's the function that removes the movie clip:

function postResponseCleanUp(): void {
    switch (lessonStep) {
        case 1 :
            break;
        case 2 :
            break;
        case 3 : 
            break;
        case 4 :

            //removeChild(screenPrint); <<previous way
            removeChild(getChildByName("screenPrintName")); // cludgy way
            removeChild(getChildByName("idaWkSheetName"));
            if (userRole == 1) { // witness
                faderOverlay.visible = false;
                instructionsCallout.callout_ta.htmlText ="<font size ='6'>The <font color='#0000FF'>Reconciler</font> continues processing the notes, repeating this process <i>for each deonmination</i>.<br><br>Click <b>Next</b> to see the next steps in the process.</font>";

            } else {
                instructionsCallout.callout_ta.htmlText ="<font size ='6'>You continue processing the notes, repeating this process <i>for each deonmination</i>.<br><br>Click <b>Next</b> to see the next steps in the process.</font>";
                }
            removeChild(pointerNew);
            idaWkSheet.removeEventListener(MouseEvent.ROLL_OVER,boardOver);
            //screenPrint.removeEventListener(MouseEvent.ROLL_OVER,boardOver);
            Mouse.show();
            break;
        case 5 : 
            break;
    }

}
1
show how you define/instantiate the mainTimeLineVariable varBadFeelingAboutThis

1 Answers

1
votes

It would be better to use the parent keyword, as the relationship between the two items is that of parent/child. Though in your case root and parent should be the same thing.

movieClipVar = MovieClip(parent).mainTimeLineVariable;

Also, with root and parent, those vars do not get populated until the object has been added to the stage (after you do addChild(object) to the object).

right before you call the line above, you should add in: trace(parent,root); and see either is null in the output window. If so, then issue is that the line of code is being called before the item has been added to the stage.

To fix that issue, you'd basically want to do this in the first frame of the child movieClip: (and not do anything else until at least frame 2)

if(!parent){
    this.addEventListener(Event.ADDED_TO_STAGE,addedToStage);
    stop();
}

function addedToStage(e:Event){
    this.removeEventListener(Event.ADDED_TO_STAGE,addedToStage);
    play();
}