Actionscript 3 AS3:1180调用可能未定义的属性:Add Child

Actionscript 3 AS3:1180调用可能未定义的属性:Add Child,actionscript-3,flash,flash-cc,Actionscript 3,Flash,Flash Cc,AS3上有一个相当大的noob。我正在尝试将SWF加载到另一个文件中。fla在鼠标单击时加载外部SWF 但是,我不断收到错误:1180调用一个可能未定义的属性:addchild 我的LoadSWF代码是: package { import flash.display.*; import flash.events.*; import flash.net.*; public class LoadSWF { private var loaderFile: Loader; pri

AS3上有一个相当大的noob。我正在尝试将SWF加载到另一个文件中。fla在鼠标单击时加载外部SWF

但是,我不断收到错误:1180调用一个可能未定义的属性:addchild

我的LoadSWF代码是:

package {
import flash.display.*;
import flash.events.*;
import flash.net.*;



public class LoadSWF {

    private var loaderFile: Loader;
    private var swfFile: Object;
    private var sourceFile: String;
    private var fileLabel: String;
    private var canDrag: Boolean;
    private var popX: int;
    private var popY: int;




    public function LoadSWF(myFile: String, myLabel: String, myX: int = 0, myY: int = 0, myDrag: Boolean = false) {
        // constructor code
        trace("Loading SWF file:", myFile);
        sourceFile = myFile;
        fileLabel = myLabel;
        canDrag = myDrag;
        popX = myX;
        popY = myY;
        openSWF();
    }

    private function openSWF(): void {
        // initialise variables      
        loaderFile = new Loader();
        swfFile = addChild(loaderFile);
        // set initial position of the SWF popup  
        loaderFile.x = popX;
        loaderFile.y = popY;
        try {
            loaderFile.load(new URLRequest(sourceFile));
            // runs after the SWF popup has finished loading     
            loaderFile.contentLoaderInfo.addEventListener(Event.COMPLETE, SWFloaded);
        } catch (err: Error) {
            // provide some error messages to help with debugging      
            trace("Error loading requested document:", sourceFile);
            trace("Error:", err);
            sourceFile = null;
        }
    } //end openSWF

    private function SWFloaded(evt: Event): void {
        // and add the required event listeners      
        loaderFile.addEventListener(MouseEvent.MOUSE_DOWN, dragSWF);
        loaderFile.addEventListener(MouseEvent.MOUSE_UP, dropSWF);
        // use the POPUP reference to access MovieClip content    
        swfFile.content.gotoAndStop(fileLabel);
        // assigns a close function to the button inside the popup    
        swfFile.content.closeBtn.addEventListener(MouseEvent.CLICK, closeSWF);
        // remove the COMPLETE event listener as it’s no longer needed      
        loaderFile.contentLoaderInfo.removeEventListener(Event.COMPLETE, SWFloaded);
    } //end SWFLOaded   


    private function dragSWF(evt: MouseEvent): void {
        swfFile.content.startDrag();
    }
    private function dropSWF(evt: MouseEvent): void {
        swfFile.content.stopDrag();
    }
    private function closeSWF(evt: MouseEvent): void {
        // remove the required event listeners first    
        loaderFile.removeEventListener(MouseEvent.MOUSE_DOWN, dragSWF);
        loaderFile.removeEventListener(MouseEvent.MOUSE_UP, dropSWF);
        swfFile.content.closeBtn.removeEventListener(MouseEvent.CLICK, closeSWF);
        // remove the pop-up    
        loaderFile.unloadAndStop();
    }


} //end class
}//结束包

我将它调用到我的主类文件中,我忽略了其余的代码,猜测它是不必要的:

package {

import flash.display.MovieClip;
import flash.text.TextField;
import flash.display.SimpleButton;
import flash.utils.Dictionary;
import flash.text.TextFormat;
import flash.net.*;
import flash.events.*;
import fl.controls.*;
import flash.media.*;
import fl.events.ComponentEvent;
import fl.managers.StyleManager;
import fl.data.DataProvider;
import fl.data.SimpleCollectionItem;
import fl.managers.StyleManager;
import fl.events.ComponentEvent;
import flash.events.Event;
import flash.net.SharedObject;
import LoadSWF;

public class Main extends MovieClip {

    //Declare variables
    private var componentFmt: TextFormat;
    private var radioBtnFmt: TextFormat;
    private var playerData: Object;
    private var savedGameData: SharedObject;
    // Pop-up Variables     
    private var popupFile: LoadSWF;
    private var swfPath: String;




    public function Main() {
        // constructor code
        this.savedGameData = SharedObject.getLocal("savedPlayerData");
        this.setComponents();
        this.setPlayerData();
        //this.swfPath = "";
        //this.isHelpOpen = false;
        //this.sndPath = "musicSFX/music2.mp3";
        //this.isMuted = false;
        //this.sndTrack = new LoadSND(this.sndPath, this.canRepeat);;
        //this.muteBtn.addEventListener(MouseEvent.CLICK, this.setMute);
        swfPath = "";
        helpBtn.addEventListener(MouseEvent.CLICK, openSWF);
        //hauntedForestBtn.addEventListener(MouseEvent.CLICK, openSWF);
    }



    public function openSWF(evt: MouseEvent): void {
        // determine which button was pressed  
        var myFile: String = evt.currentTarget.name.replace("Btn", ".swf");
        myFile = (swfPath == "") ? myFile : swfPath + myFile;
        if (myFile == "help.swf") {
            // load the help SWF file - is draggable  
            swfFile = new LoadSWF(myFile, currentFrameLabel, 80, 60, true);
        } else {
            // load the selected SWF file - is not draggable        
            swfFile = new LoadSWF(myFile, currentFrameLabel);
        }
        addChild(swfFile);
    }
如果有人能帮我找到解决办法,我将不胜感激

谢谢:

addChild由定义


因此,要访问此函数,您必须修改LoadSWF,使其扩展DisplayObjectContainer。

您的LoadSWF类需要从DisplayObject类继承。例如Sprite或Movieclip,只有这样您才能访问addChild。