Search code examples
actionscript-3navigationtimeline

AS3 Flash calling main timeline from within a movieclip within a movie clip


I've looked through similar question on this site and can't find a solution, so here is my problem:

I have a save function that saves some data.This function is in 1 movie clip in another movie clip. After the save I want to gotoAndStop(1) of the main time line not the current nested one...can anybody help?

Below is the code:

function save()
{

    var oldname:String = so.data.username;
    so.data.username = oldname + tf.text + " " + nf.text + "\n";
    tf.text = "";
    nf.text = ""; // resets textfields
    so.flush(); // writes changes to disk
    trace("Saved");
    gotoAndStop(1);  <<----this must goto frame 1 of the main time line??
}

This is AS3. In AS2 I used to be able to call _root. or _parent. and that would work fine but now it throws a compiler error. Stage.gotoAndStop(1); also doesnt work...

Appreciate any help, Thanks in advance Luben


Solution

  • You can access the topmost DisplayObject using root. Because DisplayObject does not have a gotoAndStop() method, attempting root.gotoAndStop() will result in:

    1061: Call to a possibly undefined method gotoAndStop through a reference with static type flash.display:DisplayObject.

    You can however typecast root to MovieClip1, which will grant access to it:

    MovieClip(root).gotoAndStop(1); // or:
    (root as MovieClip).gotoAndStop(1);
    

    Typecasting to MovieClip will also allow you to access user-defined properties and functions on the main timeline - this is because MovieClips are dynamic which drops compile-time constraints on what properties and methods you are allowed to access on an object.


    1Except for in cases where you have a document class that inherits Sprite instead of MovieClip.