简体   繁体   English

使用AS3加载另一个SWF文件

[英]Loading another swf file using AS3

I'm trying to load in another swf on a button click using Aaction Script 3. 我正在尝试使用Aaction Script 3在单击按钮时加载另一个swf。

The problem I'm having is that it just seems to load and mix the movies together. 我遇到的问题是,它似乎只是将电影加载并混合在一起。 Is is possible to load and replace on stage the newly loaded swf similar to how you could do this in AS2 using loadMovieNum() 可以在舞台上加载和替换新加载的swf,类似于使用loadMovieNum()在AS2中执行此操作的方式

This is what I have so far: 这是我到目前为止的内容:

//Add event listener for button click
backButton.addEventListener(MouseEvent.CLICK, backButtonClick);

//Create a function for the button click
function backButtonClick(ev:MouseEvent):void
{
var request:URLRequest = new URLRequest("2.swf");
var loader:Loader = new Loader()
loader.load(request);
addChild(loader);
}

Many thanks 非常感谢

Use the loader like this: 像这样使用加载程序:

//Add event listener for button click
var singleLoader:Loader = new Loader();
backButton.addEventListener(MouseEvent.CLICK, backButtonClick);

//Create a function for the button click
function backButtonClick(ev:MouseEvent):void
{
    var request:URLRequest = new URLRequest("2.swf");
    singleLoader.load(request);        
    addChild(loader);
}

What you're doing is you're creating a new Loader every single time for every new SWF you're loading. 您正在做的是每次为要加载的每个新SWF创建一个新的Loader。 Just use a single loader and each time you load content on it, it should replace the existing content. 只需使用一个加载程序,每次在其上加载内容时,它都应替换现有内容。 If not, adjust the code like so: 如果没有,请像这样调整代码:

function backButtonClick(ev:MouseEvent):void
{
    var request:URLRequest = new URLRequest("2.swf");
    singleLoader.unloadAndStop(true);
    singleLoader.load(request);
    addChild(loader);
}

See the documentation for more: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Loader.html 有关更多信息,请参见文档: http : //help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/flash/display/Loader.html

Update 更新

If you want to clear everything off the stage when you do this, see the following question & answer My Actionscript 3.0 Stage will not clear . 如果您要在执行此操作时清除舞台上的所有内容,请参见以下问答,“ 我的ActionScript 3.0舞台不会清除”

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

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