简体   繁体   English

Flash as3:从影片剪辑内的按钮将外部swf加载到主舞台

[英]Flash as3: Load external swf to main stage from button inside a movie clip

I have a movie clip on my main stage. 我的主要舞台上有一个电影剪辑。 There are 2 buttons. 有2个按钮。 I need the 2 buttons to load an external swf, place it on the main stage and have it centered there. 我需要2个按钮来加载外部swf,将其放在主舞台上并居中放置。

I can get the script to load them in the current movie clip but I need them to be placed on the main stage. 我可以获取将它们加载到当前影片剪辑中的脚本,但我需要将它们放置在主舞台上。

var loader_mc:Loader = new  Loader();
addChild(loader_mc);

buttoninsidemc1.addEventListener(MouseEvent.CLICK,  buttonClick)

function buttonClick(e:MouseEvent):void
{
    try {
        loader_mc.unloadAndStop();
    } catch(e:Error) {

    }

    var urlRequest : URLRequest = new  URLRequest("externalswf1.swf");
    loader_mc.load(urlRequest);
}


buttoninsidemc2.addEventListener(MouseEvent.CLICK,  buttonClick2)

function  buttonClick2(e:MouseEvent):void
{
    try {
        loader_mc.unloadAndStop();
    } catch(e:Error) {

    }

    var urlRequest : URLRequest = new  URLRequest("externalswf2.swf");
    loader_mc.load(urlRequest);
}

Code that will add loaded swf to the stage: 将已加载的swf添加到阶段的代码:

function buttonClick2(e:MouseEvent):void {
    var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
    loader.load(new URLRequest("externalswf2.swf"));
}

function onComplete(e:Event):void {
    var movie:* = LoaderInfo(e.currentTarget).content;
    //Adding content to the stage
    stage.addChild(movie);
}

Or you can change only one line of code in your realisation: 或者,您只能在实现中更改一行代码:

   stage.addChild(loader_mc);

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

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