Actionscript 3 AS3-将项添加到已加载swf中的组合框中
我已经创建了一个FlashBuilderActionScript3项目,它加载一个外部swf,该swf是在FlashCS5中创建的,其中包含combobox组件 如何向其中动态添加项目Actionscript 3 AS3-将项添加到已加载swf中的组合框中,actionscript-3,flash-builder,Actionscript 3,Flash Builder,我已经创建了一个FlashBuilderActionScript3项目,它加载一个外部swf,该swf是在FlashCS5中创建的,其中包含combobox组件 如何向其中动态添加项目 mc1['itemList'].addItem({label:"test"}); 似乎不起作用???如果要访问运行时加载的swf中的实例,可以使用getChildByName方法 Object(MovieClip(__loader.content).getChildByName('itemList')) 我使
mc1['itemList'].addItem({label:"test"});
似乎不起作用???如果要访问运行时加载的swf中的实例,可以使用getChildByName方法
Object(MovieClip(__loader.content).getChildByName('itemList'))
我使用以下代码对其进行了测试,效果良好。我创建了一个包含两个组合框的小型Flash CS5文件。第二个是演示如何实例化加载的swf中定义的类
链接到示例类和Flash CS5文件
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"});
}
}
}