AS3,在循环方法中创建更多相同的函数
AS3, create more same function in for loop mehod
我尝试在 for 循环方法中做一些函数,但它说 "Symbol 'myInstanceMovieClip', Layer 'as', Frame 31, Line 29 1084: Syntax error: expecting leftparen before leftbracket."
这是我的代码:
for(var i:int=0; i < btnhairmc.length; i++){
btnhairmc[i].buttonMode=true;
btnhairmc[i].addEventListener(MouseEvent.CLICK, goPlay[i]);
function goPlay[i](m:MouseEvent):void{
trace("btnhair[i]_mc <= clicked");
}
}
我错在哪里?
函数名称:goPlay[i]
无效,应该只是 goPlay
您可以调整您的代码:
for (var i:int=0; i < btnhairmc.length; i++) {
btnhairmc[i].buttonMode=true;
btnhairmc[i].addEventListener(MouseEvent.CLICK, goPlay);
function goPlay(m:MouseEvent):void {
trace("btnhair["+i+"]_mc <= clicked");
}
}
或者
for (var i: int = 0; i < btnhairmc.length; i++) {
btnhairmc[i].buttonMode = true;
btnhairmc[i].addEventListener(MouseEvent.CLICK, function (event: MouseEvent): void {
trace("btnhair[" + i + "]_mc <= clicked");
});
}
你可以这样做:
for (var i:int=0; i < btnhairmc.length; i++) {
btnhairmc[i].buttonMode=true;
btnhairmc[i].name="button-"+i;
btnhairmc[i].addEventListener(MouseEvent.CLICK, goPlay);
}
function goPlay(e:MouseEvent) : void {
trace(e.target.name);
//or maybe you need
trace(e.currentTarget.name);
trace('Button' + e.target.name + 'was clicked!');
}
使用 e.target 或 e.currentTarget 你会知道按下了哪个按钮
希望对您有所帮助。
我尝试在 for 循环方法中做一些函数,但它说 "Symbol 'myInstanceMovieClip', Layer 'as', Frame 31, Line 29 1084: Syntax error: expecting leftparen before leftbracket." 这是我的代码:
for(var i:int=0; i < btnhairmc.length; i++){
btnhairmc[i].buttonMode=true;
btnhairmc[i].addEventListener(MouseEvent.CLICK, goPlay[i]);
function goPlay[i](m:MouseEvent):void{
trace("btnhair[i]_mc <= clicked");
}
}
我错在哪里?
函数名称:goPlay[i]
无效,应该只是 goPlay
您可以调整您的代码:
for (var i:int=0; i < btnhairmc.length; i++) {
btnhairmc[i].buttonMode=true;
btnhairmc[i].addEventListener(MouseEvent.CLICK, goPlay);
function goPlay(m:MouseEvent):void {
trace("btnhair["+i+"]_mc <= clicked");
}
}
或者
for (var i: int = 0; i < btnhairmc.length; i++) {
btnhairmc[i].buttonMode = true;
btnhairmc[i].addEventListener(MouseEvent.CLICK, function (event: MouseEvent): void {
trace("btnhair[" + i + "]_mc <= clicked");
});
}
你可以这样做:
for (var i:int=0; i < btnhairmc.length; i++) {
btnhairmc[i].buttonMode=true;
btnhairmc[i].name="button-"+i;
btnhairmc[i].addEventListener(MouseEvent.CLICK, goPlay);
}
function goPlay(e:MouseEvent) : void {
trace(e.target.name);
//or maybe you need
trace(e.currentTarget.name);
trace('Button' + e.target.name + 'was clicked!');
}
使用 e.target 或 e.currentTarget 你会知道按下了哪个按钮
希望对您有所帮助。