本文介绍了MovieClip(root) 不工作.如何从影片剪辑访问根变量?闪存AS3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我得到了一个简单的 flash 文件,因为我试图完成从影片剪辑内的主阶段访问变量.我从谷歌找到的所有东西都指向 MovieClip(root).但它对我不起作用.

Ok Ive got a simple flash file, since im trying to accomplish accessing a variable from the main stage inside a movie clip. All the things Ive found from google point to MovieClip(root). But its not working for me.

在主时间线上:

var MyName:String;
MyName = "kenny";

然后我有一个名为 MyBox 的影片剪辑,它的代码:

Then I have a movieclip called MyBox, its code:

trace(MovieClip(root).MyName);

我收到此错误:TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Stage@2d2df089 to flash.display.MovieClip.在 MyBox/sendpmtext()

And I get this error: TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Stage@2d2df089 to flash.display.MovieClip. at MyBox/sendpmtext()

我也尝试过 MovieClip(parent)、MovieClip(parent.parent)、MovieClip(stage)、MovieClip(this.stage),但没有成功.有什么帮助吗?

I have also tried MovieClip(parent), MovieClip(parent.parent), MovieClip(stage), MovieClip(this.stage) and no luck. Any help please?

推荐答案

你可以这样做

parent["MyName"];

或者,主时间线的正确转换(在您的上下文中,父时间线的类型为 MainTimeline):

OR, a proper casting of the main timeline (in your context, the parent is of type MainTimeline):

MainTimeline(parent).MyName;

Parent 始终是一个 DisplayObjectContainer,它没有您创建的特殊属性.如果您创建自定义属性,那么您需要先转换为具有这些自定义属性的类,然后才能按名称访问它们.(否则编译器不知道它们存在,并给你那个错误).

Parent is always a DisplayObjectContainer, which has no special properties you create. If you create custom properties, then you need to cast to the class that has those custom properties before you'll be able to access them by name. (the compiler otherwise doesn't know they exist, and gives you that error).

root 指的是 swf 的最顶层.您的主时间轴实际上是 stage 的子节点,因此主时间轴上的 vars/objects/methods 不是 stage

root refers to the topmost stage of your swf. Your main timeline is actually a child of stage, so vars/objects/methods on the main timeline are not a part of stage

如果您使用 stage.addChild(MyBox),则 stage 是父时间线,而不是主时间线.如果由于某种原因您需要将 stage 作为父级,那么您必须在可以访问的地方保留对 mainTimeline 的引用.您可以在 MyBox 时间线中创建一个变量来执行此操作.

If your using stage.addChild(MyBox), then stage is the parent, and not the main timeline. If for some reason you need to have stage as the parent, then you'd have to keep a reference to the mainTimeline somewhere you can access. You could create a var to do this in your MyBox timeline.

var mainTimeline:MainTimeline;

然后在主时间线代码中,执行以下操作:

Then in the main timeline code, do this:

MyBox.mainTimeline = this;

然后您可以通过执行 mainTimeline.MyName;

Then you can access your var within MyBox by doing mainTimeline.MyName;

这篇关于MovieClip(root) 不工作.如何从影片剪辑访问根变量?闪存AS3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 19:49