Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 动态声条循环中的AddEventListener_Xml_Actionscript 3 - Fatal编程技术网

Xml 动态声条循环中的AddEventListener

Xml 动态声条循环中的AddEventListener,xml,actionscript-3,Xml,Actionscript 3,我不确定如何实现这一点,但我的问题是: 我将按钮加载到一个循环中,希望每次单击按钮时都能听到XML文件中的声音。我试图将声音对象添加到数组中,但无法动态更改声音对象的名称,因此它会覆盖所有其他对象 for(var i:int=0;i<mainclass.xml.Value.length();i++){ var button:Knopf=new Knopf(mainclass.xml.Value[i].@bez); var sound:Sound=new Sound(new

我不确定如何实现这一点,但我的问题是:

我将按钮加载到一个循环中,希望每次单击按钮时都能听到XML文件中的声音。我试图将声音对象添加到数组中,但无法动态更改声音对象的名称,因此它会覆盖所有其他对象

for(var i:int=0;i<mainclass.xml.Value.length();i++){
    var button:Knopf=new Knopf(mainclass.xml.Value[i].@bez);
    var sound:Sound=new Sound(new URLRequest(mainclass.xml.Value[i].fad));
    var array:Array=new Array();
    var array2:Array=new Array();
    start.addChild(button);
    array.push(sound);
    button.name="button"+i;
    array2.push(button.name);
    button.addEventListener(MouseEvent.CLICK, click);
    button.x=xwert;
    xwert+=100;
function click(event:MouseEvent):void {
        trace(array[0].play());
    }
}

for(var i:int=0;iEric的意思是如果在“for”中声明数组:

var array:Array=new Array();
那么数组在每个循环中都将是空的

我建议使用这段代码,请注意,我添加了一个“preparing stuff”代码块来模拟您运行代码的环境,基本上,我创建了一个包含xml属性的object mainclass,并在stage中添加了一个“start”movieclip。我还必须将Knopf类更改为一个简单的textField,但想法是一样的

为了存储声音对象,而不是数组,我使用了对象“声音”,但看看它在循环之前是如何定义的,建议使用相同的方法在循环之前初始化xwert变量

click函数也应该在循环外部定义,所有按钮只需要一个侦听器,该侦听器将知道单击的按钮是什么(event.target),然后知道其名称,我们可以使用该名称访问“sounds”对象

代码在我的计算机中工作,在同一个目录中有一个“click.mp3”声音,因为它是在我的示例xml中定义的:click.mp3

//Preparing stuff
import flash.text.TextField;
import flash.display.MovieClip;
var mainclass = {
    xml: new XML("<xml><Value bez='Button A'><fad>click.mp3</fad></Value><Value bez='Button B'><fad>click.mp3</fad></Value><Value bez='Button C'><fad>click.mp3</fad></Value></xml>")
}
var start = new MovieClip();
this.addChild(start);
//End of Pre-stuff


//Code to implement
var xwert: int = 0; //Initialize xwert variable
var sounds:Object = {}

for (var i: int = 0; i < mainclass.xml.Value.length(); i++) {
    var button: TextField = new TextField();
    button.text = mainclass.xml.Value[i].@bez;
    var sound: Sound = new Sound(new URLRequest(mainclass.xml.Value[i].fad));
    start.addChild(button);
    button.name = "button" + i;
    sounds[button.name] = sound;
    button.addEventListener(MouseEvent.CLICK, click);
    button.x = xwert;
    xwert += 100;
}
function click(event: MouseEvent): void {
    trace(sounds[event.currentTarget.name]);
    sounds[event.target.name].play();
}
//准备东西
导入flash.text.TextField;
导入flash.display.MovieClip;
var mainclass={
xml:新的xml(“click.mp3click.mp3click.mp3”)
}
var start=新的MovieClip();
这个.addChild(start);
//预处理结束
//要实现的代码
var xwert:int=0;//初始化xwert变量
变量声音:对象={}
for(var i:int=0;i

编辑:Changet target by currentTarget

你每次循环都在覆盖/重置数组是的,我知道,因为我没有动态变量名,这是主要问题。有完全相同的代码,看起来应该可以工作,但他抛出
TypeError:Error#1010:一个术语未定义,没有属性。在Soundboard/click()[C:\Users\Philipp\Adobe Flash Builder 4.6\Soundboard\src\Soundboard.mxml:41]
第41行
声音[event.target.name].play()
顺便说一句,我使用FlashBuilder而不使用Movieclip跟踪只打印未定义的声音,但不打印实例+编号。是否确定(mainclass.xml.Value[i].fad)真的在加载声音吗?我的意思是,它们是有效的URL和有效的声音文件吗?如果只跟踪声音对象会发生什么?尝试使用currentTarget而不是target。