我在联动属性中设置了15个名为“Sound1”,“Sound2”等的声音。我想在循环中动态引用它们。例如,而不是
currentMusic = new Sound2();
我该怎么做
currentMusic = new ("Sound" + i)();
或者更好的方法是什么?
答案 0 :(得分:1)
使用与在运行时加载影片剪辑或字体相同的步骤。
假设您的图书馆中的声音导出为“sound_0”,“sound_1”,...,“sound_9”等:
for(var i:uint = 0; i < 10; i++)
{
var soundClass:Class = getDefinitionByName("sound_" + i.toString()) as Class;
var sound:Sound = new soundClass();
}
答案 1 :(得分:1)
将生成的类名存储到变量中,从应用程序域中检索类并实例化新对象。 请注意例外情况!
var soundClassName:String = "Sound" + i;
var soundClass:Class;
var sound:Sound;
try {
soundClass = getDefinitionByName(soundClassName) as Class;
sound = new soundClass();
} catch (re:ReferenceError) {
trace("Class '" + soundClassName + "' not found");
} catch (te:TypeError) {
trace("Unable to instantiate the sound object");
}