ActionScript3删除子错误

时间:2015-08-27 20:08:00

标签: actionscript-3

我最近一直在将as2 fla转换为as3(AS3的新功能)并且整个工作都在导出,但是当我尝试在加载新的swf之前删除以前加载的swf时出现错误

ArgumentError: Error #2025: The supplied DisplayObject
must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at MethodInfo-11()

我知道错误与我的removeChild代码有关:

`stage.addEventListener(MouseEvent.CLICK, removeSWF);
function removeSWF (e:MouseEvent):void 
{
if(vBox.numChildren !=0){        
   // swfLoader.unloadAndStop();
    vBox.removeChild(swfLoader);// empty the movieClip memory
}
}`

但是,我似乎无法找到适合此代码的重写,但是没有错误。这段代码工作正常,所以我不确定是否值得我花时间修复这个错误,或者只是留下它。我已经搞砸了几天了,所以在这一点上我无法解决这个问题让我感到沮丧。 在这种情况下,舞台鼠标单击侦听器很有用,因为我在此代码中没有显示后退按钮,在移动到另一个场景之前清除加载的swf。 有没有人看到一个简单的解决方案,或者你认为没有必要追求,因为代码做我需要的? 完整的代码:

function launchSWF(vBox, vFile):void    {
var swfLoader:Loader = new Loader();
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);
    swfLoader.x = 165;
    swfLoader.y = 15;

    function checkLastFrame(e:Event):void { 
        if (currentSWF.currentFrame == currentSWF.totalFrames) {
        currentSWF.stop();
        // trace("DONE");     
        }
    }      
} 
var preLoader:loader = new loader();
preLoader.x = 450;
preLoader.y = 280;
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 );
        }   
//NEW ERRORS BUT WORKING
stage.addEventListener(MouseEvent.CLICK, removeSWF);
function removeSWF (e:MouseEvent):void 
{
if(vBox.numChildren !=0){        
   // swfLoader.unloadAndStop();
    vBox.removeChild(swfLoader);// empty the movieClip memory
}
}
}
var container:MovieClip = new MovieClip();
var currentSWF:MovieClip = new MovieClip();

fall_b.addEventListener(MouseEvent.CLICK, fall_bClick);
  function fall_bClick(e:MouseEvent):void {
  var swfFile:String = 'load/fall.swf';
  launchSWF(container, swfFile);
  addChild(container);
  }

face_b.addEventListener(MouseEvent.CLICK, face_bClick);
  function face_bClick(e:MouseEvent):void {
  var swfFile:String = 'load/face.swf';
  launchSWF(container, swfFile);
  addChild(container);
  }

rott_b.addEventListener(MouseEvent.CLICK, rott_bClick);
  function rott_bClick(e:MouseEvent):void {
  var swfFile:String = 'load/rottgut.swf';
  launchSWF(container, swfFile);
  addChild(container);
  }
//MORE SWFS...

任何人都有任何建议表示赞赏

2 个答案:

答案 0 :(得分:0)

首先,function launchSWF(vBox, vFile):void {未关闭。你还可以在函数内部使用函数,如果单击大括号开始和结束的行来跟踪它们就很容易解决。

我看不出你说错误的代码有什么问题,但我猜这不是所有的代码。如果您使用Flash Professisonal,则可以使用permit debugging来显示错误所在的行。

编辑:请注意,由于我正在使用移动电话编写代码,因此未对此进行测试。话虽如此,现在应该可行:

var container:MovieClip;
var currentSWF:MovieClip;

var swfFile:String;

var swfLoader:Loader;
var preLoader:Loader;

var swfURL:URLRequest;

init();

function init():void {
    preLoader = new Loader();
    preLoader.x = 450;
    preLoader.y = 280;
    vBox.addChild(preLoader);

    container = new MovieClip();
    currentSWF = new MovieClip();
    fall_b.addEventListener(MouseEvent.CLICK, fall_bClick);
    face_b.addEventListener(MouseEvent.CLICK, face_bClick);
    rott_b.addEventListener(MouseEvent.CLICK, rott_bClick);

    stage.addEventListener(MouseEvent.CLICK, removeSWF);
}

function launchSWF(vBox, vFile):void {
    swfLoader = new Loader();
    swfURL = 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);
    swfLoader.x = 165;
    swfLoader.y = 15;
}

function checkLastFrame(e:Event):void { 
    if (currentSWF.currentFrame == currentSWF.totalFrames) {
        currentSWF.stop();
        // trace("DONE");
    }      
} 

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

function removeSWF (e:MouseEvent):void {
    if(vBox.numChildren !=0){        
        //swfLoader.unloadAndStop();
        vBox.removeChild(swfLoader);// empty the movieClip memory
    }
}

function fall_bClick(e:MouseEvent):void {
    swfFile = 'load/fall.swf';
    launchSWF(container, swfFile);
    addChild(container);
}

function face_bClick(e:MouseEvent):void {
    swfFile = 'load/face.swf';
    launchSWF(container, swfFile);
    addChild(container);
}

function rott_bClick(e:MouseEvent):void {
    swfFile = 'load/rottgut.swf';
    launchSWF(container, swfFile);
    addChild(container);
}

答案 1 :(得分:0)

我改写了这个。我无法在原始代码中清除vBox错误,并且我发布了许多其他错误。 vBox代码见于教程。我认为它应该引用预加载器和swf的加载器,而vFile是实际的.swf。以下代码预加载多个swfs并清除它们而没有错误。感谢你对AntBirch的帮助。我现在开始更多地了解as3中的加载器了。

//LOAD FIRST PIECE ON OPEN (required to removeChild later)
   var swfLoader:Loader = new Loader();
   var defaultSWF:URLRequest = new URLRequest("load/fall.swf");
   swfLoader.load(defaultSWF);
   swfLoader.x = 165;
   swfLoader.y = 15;
   addChild(swfLoader);
//PRELOADER
   var preLoader:loader = new loader();
   preLoader.x = 450;
   preLoader.y = 280;

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

function onProgressHandler(event:ProgressEvent){
   var dataAmountLoaded:Number=event.bytesLoaded/event.bytesTotal*100;
   preLoader.lpc.text= int(dataAmountLoaded)+"%";
}
//BUTTONS
function btnClick(event:MouseEvent):void {

   swfLoader.unloadAndStop();
   removeChild(swfLoader);
   swfLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,      onProgressHandler);
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,  loadProdComplete);    
addChild(preLoader);    
var newSWFRequest:URLRequest = new URLRequest("load/" + event.target.name +    ".swf");
swfLoader.load(newSWFRequest);
swfLoader.x = 165;
swfLoader.y = 15;;
addChild(swfLoader);
}

// BUTTON LISTENERS
fall.addEventListener(MouseEvent.CLICK, btnClick);  
face.addEventListener(MouseEvent.CLICK, btnClick);
rott.addEventListener(MouseEvent.CLICK, btnClick);  
angel.addEventListener(MouseEvent.CLICK, btnClick);
ratts.addEventListener(MouseEvent.CLICK, btnClick); 
metal.addEventListener(MouseEvent.CLICK, btnClick);
//etc...    


//BACK BUTTON
BB3.addEventListener(MouseEvent.CLICK, BB3Click);

function BB3Click(e:MouseEvent):void {
   swfLoader.unloadAndStop();
   removeChild(swfLoader);
   this.gotoAndPlay(1 ,"Scene 2")
}