简体   繁体   English

AS3-使用空加载器时添加外部.swf预加载器

[英]AS3 - Adding an external .swf preloader when using null loader

Got a question for you all! 给大家一个问题! I'm trying to make a navigation system for my website where each nav item is loaded as an external .swf. 我正在尝试为我的网站创建一个导航系统,其中每个导航项都作为外部.swf加载。 Thanks to another user on this site, I was able to get that functionality in place. 感谢此站点上的另一个用户 ,我能够使用该功能。 The problem is I want to add a preloader for these external swfs as well. 问题是我也想为这些外部swfs添加预加载器。

The code I'm working with is this: 我正在使用的代码是这样的:

var loadedSWF:Loader = null;

/**
 * Loads an SWF and adds it to container once complete
 * @param file The URL to the SWF to load
 * @param container The container to add the SWF to
 */
function loadSWF(file:String, container:MovieClip=null):void
{   
    if(container == null) container = MovieClip(root);

    // removes the previously loaded SWF
    if(loadedSWF != null)
    {
        if(loadedSWF.parent) loadedSWF.parent.removeChild(loadedSWF);
    }

    var req:URLRequest = new URLRequest(file);
    loadedSWF = new Loader();
    loadedSWF.load(req);

    addChild(loadedSWF);
}



menu_mc.test_btn.addEventListener(MouseEvent.CLICK, _click);
function _click(e:MouseEvent):void
{
    loadSWF("testmovie.swf");
    loadedSWF.x = 0;
    loadedSWF.y = 125;
    var otherindex = getChildIndex(Border);
    setChildIndex(loadedSWF, otherindex + 1);
}

So far everything I've tried has come up short. 到目前为止,我尝试过的所有内容都不多。 I can get a preloader working if I don't use a null loader, but when I do that I'm not sure how to get it to remove the assets being used when loading another .swf - everything just stacks and bogs down the site. 如果不使用null加载程序,则可以使preloader正常工作,但是当我这样做时,我不确定如何在加载另一个.swf时如何使用它来删除正在使用的资产-所有内容都会在该网站上堆积并造成沼泽。 If I try and put together a preloader attached to the above code, I get errors because I'm making calls to a null object. 如果我尝试将附加到上述代码的预加载器放在一起,则会出错,因为我正在调用null对象。 Sorry to be such a newbie, I've only just begun to wrap my head around flash. 很抱歉成为这样的新手,我才刚刚开始将头缠在闪光灯上。 I appreciate any help! 感谢您的帮助!

Edit: Here's a quick look at my somewhat jumbled version that includes a preloader, but doesn't work due to the null loader: 编辑:这是我有点混乱的版本的快速浏览,其中包括一个预加载器,但由于空加载器而无法使用:

var loadedSWF:Loader = null;

/**
 * Loads an SWF and adds it to container once complete
 * @param file The URL to the SWF to load
 * @param container The container to add the SWF to
 */
function loadSWF(file:String, container:MovieClip=null):void
{   
    if(container == null) container = MovieClip(root);

    // removes the previously loaded SWF
    if(loadedSWF != null)
    {
        if(loadedSWF.parent) loadedSWF.parent.removeChild(loadedSWF);
    }

    loadedSWF.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
    loadedSWF.contentLoaderInfo.addEventListener(Event.COMPLETE, loadProdComplete);
    var req:URLRequest = new URLRequest(file);
    loadedSWF = new Loader();
    loadedSWF.load(req);

    function loadProdComplete(e:Event):void {
    trace("swf file loaded");   
    //remove the preloader from container clip
            removeChild(preLoader);

           // add the loaded swf to container clip
    addChild(loadedSWF);    

    currentSWF = MovieClip(loadedSWF.content);
    currentSWF.gotoAndPlay(1);

    currentSWF.addEventListener(Event.ENTER_FRAME , checkLastFrame);

function checkLastFrame(e:Event):void { 

if (currentSWF.currentFrame == currentSWF.totalFrames) {
     currentSWF.stop();
    // trace("stopped");     
   }

   }   

}

var preLoader:loader = new loader();

//position the loading bar
preLoader.x = 155;
preLoader.y = 185;

addChild(preLoader);

function onProgressHandler(event:ProgressEvent){

var dataAmountLoaded:Number=event.bytesLoaded/event.bytesTotal*100;
preLoader.bar.scaleX = dataAmountLoaded/100;
preLoader.lpc.text= int(dataAmountLoaded)+"%";

trace(preLoader.bar.scaleX );

   }

}

var currentSWF:MovieClip = new MovieClip();


menu_mc.test_btn.addEventListener(MouseEvent.CLICK, _click);
function _click(e:MouseEvent):void
{
    loadSWF("testmovie.swf");
    loadedSWF.x = 0;
    loadedSWF.y = 125;
    var otherindex = getChildIndex(Border);
    setChildIndex(loadedSWF, otherindex + 1);
}

Edit #2: Functional code, but now receiving the following output error whenever I hit the button. 编辑#2:功能代码,但是现在每当我按下按钮时都会收到以下输出错误。 The swfs load and unload just fine, but if anyone knows how I could clean it up to avoid the output error I'd like to get it as smooth as possible!: swfs的加载和卸载都很好,但是如果有人知道如何清理它以避免输出错误,我想使其尽可能平滑!

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. ArgumentError:错误#2025:提供的DisplayObject必须是调用方的子级。 at flash.display::DisplayObjectContainer/removeChild() at MethodInfo-8() 在flash.display :: DisplayObjectContainer / removeChild()在MethodInfo-8()

var container:MovieClip = new MovieClip();
var currentSWF:MovieClip = new MovieClip();
var swfLoader:Loader = new Loader();

function launchSWF(vBox, vFile):void{   

//vBox.addChild(swfLoader);
var swfURL:URLRequest = new URLRequest(vFile);

swfLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadProdComplete);

swfLoader.load(swfURL);

function loadProdComplete(e:Event):void {
    trace("swf file loaded");   
    vBox.removeChild(preLoader);
    vBox.addChild(swfLoader);   

    currentSWF = MovieClip(swfLoader.content);
    currentSWF.gotoAndPlay(1);


    currentSWF.addEventListener(Event.ENTER_FRAME , checkLastFrame);

function checkLastFrame(e:Event):void { 

    if (currentSWF.currentFrame == currentSWF.totalFrames) {
     currentSWF.stop();
    // trace("DONE");     
   }

   }   

}

var preLoader:loader = new loader();
preLoader.x = 155;
preLoader.y = 185;

vBox.addChild(preLoader);

function onProgressHandler(event:ProgressEvent){

var dataAmountLoaded:Number=event.bytesLoaded/event.bytesTotal*100;
preLoader.bar.scaleX = dataAmountLoaded/100;
preLoader.lpc.text= int(dataAmountLoaded)+"%";

trace(preLoader.bar.scaleX );

   }    

}

test_btn.addEventListener(MouseEvent.CLICK, _load); 

function _load(e:Event):void{

     swfLoader.unloadAndStop();
     var swfFile:String = 'test.swf';
     launchSWF(container, swfFile);
     //put it on stage
     addChild(container);
}

不要使用null,而只需使用Loader类的unload方法。

Loader.unload()

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

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