AS3:1180调用可能未定义的属性:添加子项

时间:2014-10-26 12:22:40

标签: actionscript-3 flash flash-cc

在AS3上相当大的菜鸟。我正在尝试将SWF加载到另一个文件中。 (点击鼠标时主要.fla加载外部SWFS)

我一直收到错误:1180调用可能未定义的属性:然而,添加Child。

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

如果有人能帮我找到解决方案,我会非常感激。

谢谢:)

1 个答案:

答案 0 :(得分:0)

addChildDisplayObjectContainer定义。

因此,要访问此功能,您必须更改LoadSWF以使其extends DisplayObjectContainer