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