我动态创建了Sound
对象
var files:Object={};
files["file1"]={};
files["file1"]["snd"]=new Sound();
...... //url etc
files["file1"]["id"]='01'; //that's important
files["file1"]["snd"].addEventListener(Event.COMPLETE, onSoundLoaded);
function onSoundLoaded(event:Event):void
//// somehow I need to get the `id` property of its parent
}
我试过
var localRef:Object = event.target as Object;
var localRef:Object = event.target.parent as Object;
但它会引发错误。
所以我需要从files["file1"]["id"]
函数中获取onSoundLoaded()
。有可能吗?
答案 0 :(得分:2)
严格回答你的问题,你的" snd"之间没有等级关系。对象和你的" file1"宾语。 parent关键字用于显示对象层次结构。
您必须遍历文件对象才能找到匹配项:
//in the sound loaded function
for(var f:Object in files){
if(f["snd"] === event.currentTarget){
var myId:String = f["id"];
//do something with the id
break; //don't keep looping
}
}
为了超越问题的范围,使用包含与该声音相关的所有属性的自定义类/模型确实会更清晰,更容易。比如这样的东西:( swf / fla旁边的一个名为MySound.as
的文件,包含以下内容:
package {
import flash.media.Sound;
import flash.media.SoundLoaderContext;
import flash.net.URLRequest;
public class MySound extends Sound {
public var id:String;
public function MySound(id_:String, stream:URLRequest, context:SoundLoaderContext):void {
id = id_;
//call the original constructor method for Sound
super(stream, context);
}
}
}
现在,你可以这样做:
//use a vector/array instead of an object
var sounds:Vector.<MySound> = new Vector.<MySound>();
var sound:MySound = new MySound("01");
sound.addEventListener(Event.COMPLETE, onSoundLoaded);
//add to sound array
sounds.push(sound);
function onSoundLoaded(e:Event):void {
var sound:MySound = MySound(e.currentTarget);
trace("My ID: ", sound.id);
}
根据您正在做的事情,您可以收听并回复MySound类中的完整事件(已加载),进一步抽象出来。