简体   繁体   中英

Load External SWF through ComboBox AS3

I am trying to create a ComboBox that loads and unloads an external SWF onto stage using ActionScript 3.0 using Flash CS5.

Currently there are 2 list items in the combo box: Home and About. Upon selecting Home or About option from ComboBox it displays both Home and About SWF at once when selected.

I only want 1 SWF to be displayed only when selected, not all.

menuList.addItem({label:"Choose"});
menuList.addItem({label:"Home",path:"home_load.swf"});
menuList.addItem({label:"About",path:"about.swf"});

menuList.addEventListener(Event.CHANGE, Home);
menuList.addEventListener(Event.CHANGE, About);

var loader:Loader = new Loader();
loader.unloadAndStop();


function Home(e:Event):void
{
    if (e.currentTarget.selectedItem.path)
    {
        var loader:Loader = new Loader();
        //loader.unloadAndStop();
        loader.load(new URLRequest("home_load.swf"));

        addChild(loader);
        //loader.unloadAndStop();
        loader.x = 0;
        loader.y = 190;
    }
}

function About(e:Event):void
{
    if (e.currentTarget.selectedItem.path)
    {
        //loader.unloadAndStop();
        var loader:Loader = new Loader();
        loader.load(new URLRequest("about.swf"));

        addChild(loader);
        //loader.unloadAndStop();
        loader.x = 0;
        loader.y = 190;
    }
}

You may have to do addChild after the swf file has completed loading.

//These listeners detect when the file has finished loading, and if the
//correct file is loaded.
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, finishLoading);
swfLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);

//The load method then loads the SWF file into the loader object.
swfLoader.load(my_url);

//This function adds the external SWF to the stage.
function finishLoading(loadEvent:Event) {
addChild(loadEvent.currentTarget.content);
}

//This function displays a message if the file is not found.
function errorHandler(errorEvent:Event):void {
trace("file missing");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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