1
votes

I want to remove and re-add/"restart" the Main class when it finishes animating. All of my animations are happening in Main and are added to the display tree inside Main. Program.as handles all this by adding/removing Main. How can I run the finishNow() function inside Main.as? It all works fine, except throws an error for program.finishNow();:

TypeError: Error #1009: Cannot access a property or method of a null object reference.

The .fla file:

Linked to Program.as.

The Program.as file:

package  {

    import flash.display.MovieClip;

    public class Program extends MovieClip {


        public function Program() {

            startNow();
        }

        function startNow() {
            var run:Main = new Main(this);
            addChild(run);
        }

        function finishNow() {
            removeChild(run);
            var run:Main = new Main(this);
            addChild(run);
        }

    }

}

The Main.as file:

package {

    import flash.display.Sprite;

    public class Main extends Sprite
    {

        var stageHolder:Object;
        public var program:Program;

        public function Main(stageHolderTemp) {
            stageHolder = stageHolderTemp; 
            trace(stageHolder);

            someFunctionsThatDrawGraphics();
        }

        function callFinishFunction():void {
            // how to call finishNow() function from Program.as file here?
            program.finishNow();
        }

    }
}
2

2 Answers

2
votes

You should have your run variable class-wide, not function-wide.

package  {
    import flash.display.MovieClip;
    public class Program extends MovieClip {
    var run:Main; // <- THIS line
        public function Program() {
            startNow();
        }
        function startNow() {
            run = new Main(this); // and no 'var' here
            addChild(run);
        }
        function finishNow() {
            removeChild(run);
            run = new Main(this); // also no 'var' here
            addChild(run);
        }
    }
}
0
votes

The fix to TypeError: Error #1009: Cannot access a property or method of a null object reference. can be found below. Previously the class was pointing to a null object.

package {

    import flash.display.Sprite;

    public class Main extends Sprite
    {

        // var stageHolder:Object; <- REMOVE this line
        public var program:Program;

        public function Main(stageHolderTemp) {
            program = stageHolderTemp; // changed stageHolder to program
            trace(program); // changed stageHolder to program

            someFunctionsThatDrawGraphics();
        }

        function callFinishFunction():void {
            program.finishNow();
        }

    }
}