0
votes

My Main class is added to the stage of my .fla and I want to remove and re-add/"restart" the class when it finishes animating. All of my animations are happening in Main and are added to the display tree inside Main. How can I run the finishNow() function from within Main.as?

The .fla file:

var run:Main = new Main(this);
stage.addChild(run);

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

The Main.as file:

var stageHolder:Object;

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

function callFinishFunction():void {
    // how to call finishNow() function from .fla file here
}

EDIT: The design of the program has changed. Still trying to do the same thing (call finishNow() function – but it is now in Program.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:

It now does not contain any code. It is 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();
        }

    }
}
3

3 Answers

0
votes

You can call addframeScript to call FLA function.

0
votes

If your .fla linked to Main.as file, you can directly called. try this:

public function Main() {
        finishNow();
}

enter image description here

.fla code

function testFunction()
{
    trace("111");
}

.as file

package  {

    import flash.display.MovieClip;


    public class Main extends MovieClip {


        public function Main() {

            testFunction();
        }
    }

}
0
votes

Actually , no need to create the class object every time . It will be enough to call a function in that class every time .

But for your question .. Try this ....

your Document Class

In program.as

    var run:Main = new Main(this);
    run.addEventListener("FINISH",finishNow);
    addChild(run);
    function finishNow(e:Event)
    {
     }

In main.as ,

function callFinishFunction():void
 {
  dispatchEvent(new Event("FINISH"));
 }