简体   繁体   English

AS3 Flash 从影片剪辑中的影片剪辑中调用主时间线

[英]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.我有一个保存 function 可以保存一些数据。这个 function 在另一个电影剪辑中的一个电影剪辑中。 After the save I want to gotoAndStop(1) of the main time line not the current nested one...can anybody help?保存后,我想转到主时间线的 gotoAndStop(1) 而不是当前嵌套的...有人可以帮忙吗?

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.这是AS3。 In AS2 I used to be able to call _root.在 AS2 中,我曾经能够调用 _root。 or _parent.或_parent。 and that would work fine but now it throws a compiler error.这可以正常工作,但现在它会引发编译器错误。 Stage.gotoAndStop(1); Stage.gotoAndStop(1); also doesnt work...也不起作用...

Appreciate any help, Thanks in advance Luben感谢任何帮助,在此先感谢 Luben

You can access the topmost DisplayObject using root .您可以使用root访问最顶层的DisplayObject Because DisplayObject does not have a gotoAndStop() method, attempting root.gotoAndStop() will result in:因为DisplayObject没有gotoAndStop()方法,所以尝试root.gotoAndStop()将导致:

1061: Call to a possibly undefined method gotoAndStop through a reference with static type flash.display:DisplayObject. 1061:通过 static 类型 flash.display:DisplayObject 的引用调用可能未定义的方法 gotoAndStop。

You can however typecast root to MovieClip 1 , which will grant access to it:但是,您可以将root 类型转换MovieClip 1 ,这将授予对它的访问权限:

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.MovieClip进行类型转换还允许您在主时间轴上访问用户定义的属性和函数 - 这是因为 MovieClip 是dynamic的,它放弃了编译时对允许您在 object 上访问哪些属性和方法的限制。


1 Except for in cases where you have a document class that inherits Sprite instead of MovieClip . 1除非您有一个继承Sprite而不是MovieClip的文档 class 的情况。

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);我不会在 Flash 时间轴上进行大量编码(如果您的应用程序甚至具有中等复杂性,我建议您开始研究通过文档根目录使用外部 class 定义); 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.在 AS3 中,在显示列表上调度的事件可以将其bubbles属性设置为 true,这将启用事件冒泡。 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:在您的应用程序中,假设您有两个“块”操作,即save function 定义和main function 定义:

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));

//Hence you can go to the first frame of your Scene 1 //因此您可以 go 到场景 1 的第一帧

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM