2
votes

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

3

3 Answers

5
votes

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.

1
votes

I don't do a lot of coding on the Flash Timeline (and I suggest that you start looking into using external class definitions via the Document Root if your application is even of medium complexity); but the following suggest should still hold true.

In AS3, Events dispatched on the Display List can have their bubbles property set to true which will enable event bubbling. By enabling event bubbling you can listen for the even higher up on the display list, the following article does a great job of explaining it with a demo you can play with.

In your application let's assume you have two "chunks" of actions, the save function definition and the main function definition:

Main

// Add an event listener, when we hear a 'SaveEvent' we will call
// the onSaveEvent function.
addEventListener("SaveEvent", onSaveEvent);

// This function is called when we hear a 'SaveEvent'.
function onSaveEvent(event : Event) : void {
    trace("Main heard event: " + event.type);

    // We can now instruct our Main Timeline.
    gotoAndStop(1);
}

Save

// Perform your save operation as before...
so.flush(); // writes changes to disk
trace("Saved");

// Now dispatch an Event, make sure we set it to bubble.
var bubbles : Boolean = true;
dispatchEvent(new Event("SaveEvent", bubbles));
0
votes

//Hence you can go to the first frame of your Scene 1

MovieClip(root).gotoAndStop(1, "Scene 1");