简体   繁体   English

加载和使用SWF文件

[英]Loading and using SWF files

I'm new to AS3, and am trying to understand how externally loaded SWFs work in AS3. 我是AS3的新手,我正在尝试了解外部加载的SWF如何在AS3中运行。 Since Flash 4/5, it was common to have one main SWF file in a Flash web project, and then load other SWF files into it, often for various "sections" of a website or web project. 从Flash 4/5开始,通常在Flash Web项目中有一个主SWF文件,然后将其他SWF文件加载到其中,通常用于网站或Web项目的各个“部分”。 In the main file, we'd have masks animating the container movieclip(in which external sections/SWF files were loaded) and have animations and transitions play as the section finished loading and the loaded content was displayed. 在主文件中,我们有动画容器动画片段(其中加载了外部部分/ SWF文件)的动画,并在部分完成加载并显示加载的内容时播放动画和过渡。

In AS3, I've used the Loader class to load and display the external file, my main problem is in communicating with the loaded content, call it's functions, or call root functions from it. 在AS3中,我使用Loader类来加载和显示外部文件, 我的主要问题是与加载的内容进行通信,调用它的函数或从中调用根函数。

In AS2, we could use someMovieClip.loadMovie("ExternalContent.swf") and the ExternalContent file would load inside someMovieClip. 在AS2中,我们可以使用someMovieClip.loadMovie(“ExternalContent.swf”),ExternalContent文件将加载到someMovieClip中。 You could access functions on the "External.swf" main timeline using someMovieClip.function();. 您可以使用someMovieClip.function();访问“External.swf”主时间轴上的函数。 And inside the "ExternalContent.swf", we could use _root.function() to access functions in the main file ExternalContent was being loaded into. 在“ExternalContent.swf”中,我们可以使用_root.function()来访问正在加载的ExternalContent主文件中的函数。 Doing this in AS3 seems bizarre and neurotic, and I feel like I'm missing something fairly basic here. 在AS3中这样做似乎很奇怪和神经质,我觉得我在这里缺少一些相当基本的东西。

//Loading in ExternalContent.swf into the sprite
//ExternalContent has a movieclip called "boxes" on it's main timeline
//boxes has a boxesPrompt() function in it's timeline.

var sprite:Sprite = new Sprite();
addChild(sprite);

var loader:Loader = new Loader();
loader.load(new URLRequest("ExternalContent.swf"));

loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaded);

function onLoaded(event:Event):void
{
    sprite.addChild(event.target.content);

        sprite.boxes.boxesPrompt();
        //Flash gives the following compiler error at the above
        //Scene 1, Layer 'Layer 1', Frame 1, Line 21 1119: Access of possibly undefined property boxes through a reference with static type flash.display:Sprite.

        //But when I comment out sprite.boxes.boxesPrompt() and use this, it works:
        event.target.content.boxes.boxesPrompt()
}

The boxesPrompt() function inside the "ExternalContent.swf" just traces it's parent, grand-parent, and great grand-parent - trace(this.parent.parent.parent);. “ExternalContent.swf”中的boxesPrompt()函数只跟踪它的父,祖父和伟大的祖父 - 跟踪(this.parent.parent.parent);. And when I call that function inside the onLoaded event-handler using "event.target.content.boxes.boxesPrompt()", it shows that the Boxes object(which was on the main timeline of External.SWF), has a parent movieclip, a grand-parent sprite, and a great grand-parent object mainTimeline. 当我使用“event.target.content.boxes.boxesPrompt()”在onLoaded事件处理程序中调用该函数时,它显示Boxes对象(位于External.SWF的主时间轴上)具有父动画片段,一个祖父精灵,一个伟大的祖父对象mainTimeline。

I thought re-parenting the loaded content into the sprite would allow me to access the loaded content as easily as loadMovie() used to be - accessing loaded content like it was present directly inside the clip it was loaded in. But that doesn't work at all. 我认为将加载的内容重新绑定到精灵中将允许我像loadMovie()一样容易访问加载的内容 - 访问加载的内容,就像它直接存在于加载的剪辑中一样。但是这不是工作。

So to rephrase, my question is: 所以重新说一下,我的问题是:

  • How do I communicate from the main "loader" SWF file, with the content that's loaded in. I don't want to communicate using event.target.content.{etc} because then I'd only be able to address the loaded content inside the Loader's event.complete event handler. 如何从主“加载器”SWF文件与加载的内容进行通信。我不想使用event.target.content。{etc}进行通信,因为那时我只能处理加载的内容在Loader的event.complete事件处理程序中。

  • How do I "re-parent" loaded content, so I can place it inside some movieclip/sprite on the main timeline of the loader file, rather than using some really long convoluted way. 我如何“重新加载”加载的内容,因此我可以将它放在加载程序文件的主时间轴上的某个movieclip / sprite中,而不是使用一些非常冗长的方式。

  • How to communicate from inside the loaded content, to the main/loader file. 如何从加载的内容内部与main / loader文件进行通信。 Previously, we'd use _root.functionName() to do stuff like play some animation transitioning from the current externally loaded "section" to another section. 以前,我们使用_root.functionName()来做一些事情,比如播放从当前外部加载的“部分”转换到另一部分的动画。 How'd I go about doing that. 我怎么去做那件事。

AS2 & AS3 is vastly different. AS2和AS3有很大的不同。 But you will have to swallow the fact that AS3 has been developed as an improvement over AS2. 但是你必须承认AS3已经发展成为对AS2的改进。 So any transition you make, is also for the better. 因此,您所做的任何转变也都会变得更好。

For eg : The _root in AS2 allowed global objects & variables to accessed & changed anywhere, which is a bad practice & leads to non maintainable code in a project. 例如:AS2中的_root允许在任何地方访问和更改全局对象和变量,这是一种不好的做法,导致项目中不可维护的代码。

Having said that, let me address your questions: 话虽如此,让我来解答你的问题:

  • If you are able to get access to the loaded content with event.target.content ... you should save it inside a,say class variable & may access it later elsewhere in the class. 如果您能够使用event.target.content访问加载的内容...您应该将其保存在一个类似的变量中,并可以在类的其他地方访问它。

    You must understand that you will be able to access the content only after loading it, so have to wait for it to complete anyway & event.complete handler is probably your best bet. 您必须明白,只有在加载后才能访问内容,因此必须等待它完成并且event.complete处理程序可能是您最好的选择。

  • I doubt if you can pick random content from a loaded swf & re-parent it into the current swf.As explained you might not have a long convoluted way. 我怀疑你是否可以从加载的swf中选择随机内容并将其重新加入到当前的swf中。如上所述,你可能没有一个漫长的复杂方式。

  • Accessing the parent could be done in many ways. 可以通过多种方式访问​​父级。 You can use .parent or actually call a function from the parent swf passing its reference to the child. 您可以使用.parent或实际调用父swf中的函数,将其引用传递给子项。


var sprite;
addChild(sprite);

var loader:Loader = new Loader();
loader.load(new URLRequest("ExternalContent.swf"));

loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaded);

function onLoaded(event:Event):void
{
        sprite = event.target.content;

        //This should work
        sprite.boxes.boxesPrompt();
}

See this example for more info. 有关详细信息,请参阅此示例

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

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