简体   繁体   English

如何从外部SWF控制主时间轴

[英]How to control main timeline from external swf

I have a main.swf and inside it is just a plain container movieclip. 我有一个main.swf,里面只有一个普通的容器动画片段。 Using the Loader function I load an external swf called content.swf inside that container. 使用Loader函数,我在该容器中加载了一个名为content.swf的外部swf。 Container.swf has a button inside it. Container.swf内部有一个按钮。 Now when external swf is loaded in container mc which is on the main.swf stage, I want to move to another frame by clicking the button of the loaded external swf. 现在,当将外部swf加载到位于main.swf阶段的容器mc中时,我想通过单击已加载的外部swf的按钮移至另一帧。 If I try to control it from the main timeline I get error saying 如果我尝试从主时间轴进行控制,则会收到错误提示

cannot convert flash.display::Loader@117e7041 to flash.display.MovieClip.

If I try to control it from the external swf I can trace the click on the button but timeline doesn't move to the wanted frame. 如果尝试从外部swf控制它,则可以跟踪按钮的单击,但时间轴不会移至所需的帧。

From the error message it seems that you are trying to cast a loader into a movie clip. 根据错误消息,您似乎正在尝试将加载程序投射到影片剪辑中。 Use the loader.content property instead. 请改用loader.content属性。

var mc:MovieClip = loader.content as MovieClip;
//do whatever you want with mc

I did it like this: 我这样做是这样的:

var loaderPath:URLRequest = new URLRequest("content.swf");
var loader:Loader = new Loader();
loader.load(loaderPath);
var mc:MovieClip = loader.content as MovieClip;
container.addChild(mc);

and after this I receive this error: 然后,我收到此错误:

TypeError: Error #2007: Parameter child must be non-null. TypeError:错误#2007:参数child必须为非null。 at flash.display::DisplayObjectContainer/addChild() at index_fla::MainTimeline/frame1() 在flash.display :: DisplayObjectContainer / addChild()在index_fla :: MainTimeline / frame1()

Make sure you're using event handlers as shown here because in your case loader.content has not loaded 'content.swf' yet when you try to add it to your container. 确保使用的是此处所示的事件处理程序,因为在这种情况下,当您尝试将loader.content添加到容器时,loader.content尚未加载“ content.swf”。

Also it is preferable to cast like this: 另外,最好像这样进行转换:

var mc:MovieClip = MovieClip(loader.content);

For the simple reason that 'as' won't throw you an error is the object you're trying to cast is null or can't be casted. 由于“ as”不会抛出错误的简单原因是,您要投射的对象为null或无法投射。

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

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