AS3 - Adding item to a combobox in a loaded swf

会有一股神秘感。 提交于 2019-12-12 02:37:30

问题


I've created a flash builder actionscript 3 project which loads an external swf which was created in flash cs5 which contains the combobox component.

How do i dynamically add items to it?

mc1['itemList'].addItem({label:"test"});

does not seem to work??


回答1:


if you want to access instances within a swf loaded at runtime you can use the getChildByName method

Object(MovieClip(__loader.content).getChildByName('itemList'))

I tested it using the following code, it works fine. I created a small Flash CS5 File containing two ComboBoxes. The second one is to demonstrate how to instantiate a class defined within the loaded swf.

Link to the example Class and the Flash CS5 File http://public.goldsource.de/stackOverflow/ComboBoxTest.zip

package
{
    import flash.display.Loader;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.net.URLRequest;
    import flash.utils.describeType;
    import flash.utils.getDefinitionByName;

    public class ComboBoxTest extends Sprite
    {

        private var __loader:Loader = new Loader();
        public function ComboBoxTest()
        {   
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;              
            __loader.contentLoaderInfo.addEventListener(Event.COMPLETE,__onComplete);   

            /*
                Within the ComboBoxContainer.swf you find ComboBox-Component named 'myComboBox'.
                There is another ComboBox within a MovieClip that is exported as MyComboBoxClass. This is necessary if you
                want to add more than one ComboBox without loading the swf again.
            */          
            __loader.load(new URLRequest('assets/ComboBoxContainer.swf'));
        }


        private function __onComplete($e:Event):void{

            /* You can even access the ComboBox while within Loader. 
               this line adds a new item*/
            Object(MovieClip(__loader.content).getChildByName('myComboBox')).addItem({label:"First Box"});

            /*  
                I suggest to get rid of the loader. The addChild is not necessary to fetch a reference,
                i used it to add the ComboBox to the stage. Because addChild returns the reference storing it is
                possible within the same line.
            */
            var importedComboBox:Object = addChild(MovieClip(__loader.content).getChildByName('myComboBox'));
            importedComboBox.y = 20;
            importedComboBox.x = 10;
            importedComboBox.addItem({label:"Some Item"});


            /*
                By the way, you can also extract the class Definiton. So its possible to instantiate the ComboBox.

            */
            var myComboBoxClass:Class = __loader.contentLoaderInfo.applicationDomain.getDefinition("MyComboBoxClass") as Class; 


            // You can instantiate this class multiple times
            var mySecondComboBox:Object = addChild(new myComboBoxClass());  
            mySecondComboBox.y = 60;
            mySecondComboBox.x = 10;
            mySecondComboBox.getChildByName('comboBox').addItem({label:"Second Box"});


            var myThirdComboBox:Object = addChild(new myComboBoxClass());   
            myThirdComboBox.y = 100;
            myThirdComboBox.x = 10;
            myThirdComboBox.getChildByName('comboBox').addItem({label:"Third Box"});
        }
    }
}


来源:https://stackoverflow.com/questions/6535038/as3-adding-item-to-a-combobox-in-a-loaded-swf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!