AS3 - 添加项目到一个ComboBox加载的SWF加载、项目、SWF、ComboBox

2023-09-08 15:21:04 作者:媳妇为王°

我已经创建了一个Flash Builder ActionScript 3的项目,它加载包含ComboBox组件这是在闪存CS5创建的外部SWF。

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??

推荐答案

如果你想在运行时可以使用getChildByName方法加载的SWF中访问实例

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

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

我测试了它使用下面的code,它工作正常。我创建了一个包含两个组合框一个小的Flash CS5文件。第二个是展示如何实例加载的SWF中定义的类。

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.

链接到该示例类和Flash CS5文件 http://public.goldsource.de/stackOverflow/ComboBoxTest.zip

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