AS3 - 使用null加载程序时添加外部.swf预加载器

时间:2012-03-24 04:45:08

标签: actionscript-3 flash flash-cs5.5

大家都有问题!我正在尝试为我的网站制作导航系统,其中每个导航项都作为外部.swf加载。感谢此网站上的another user,我能够获得该功能。问题是我想为这些外部swfs添加预加载器。

我正在使用的代码是:

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);
}

到目前为止,我所尝试的一切都很短暂。如果我不使用null加载器,我可以得到预加载器,但是当我这样做时,我不知道如何在加载另一个.swf时删除正在使用的资产 - 一切只是堆栈并陷入网站。如果我尝试将上面代码附加的预加载器放在一起,我会收到错误,因为我正在调用null对象。很抱歉成为这样的新手,我才刚刚开始围绕闪光灯。我感谢任何帮助!

编辑:这里有一个快速浏览我的有点混乱的版本,包括一个预加载器,但由于空加载器不起作用:

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);
}

编辑#2:功能代码,但每当我按下按钮时,现在都会收到以下输出错误。 swfs加载和卸载就好了,但如果有人知道我怎么能清理它以避免输出错误我想尽可能顺利!:

ArgumentError:错误#2025:提供的DisplayObject必须是调用者的子级。 在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);
}

1 个答案:

答案 0 :(得分:0)

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

Loader.unload()