2012-04-11 97 views
1

我有按钮的列表:AS3阵列按钮嵌套

agreeButton disagreeButton container.clickButton1 container.clickButton2

Container是另一个影片剪辑和它里面是最后的2个按钮。

我该如何把它放在数组中,并将所有相同的监听器应用到它们中的每一个?

var buttonArray:Array = new Array("agreeButton", "disagreeButton", "container.clickButton1", "container.clickButton2"); 
for (var i:int=0; i<buttonArray.length; i++) { 
    this[buttonArray[i]].addEventListener(MouseEvent.ROLL_OVER, mouseRollOver); 
    this[buttonArray[i]].addEventListener(MouseEvent.ROLL_OUT, mouseRollOut); 
    this[buttonArray[i]].addEventListener(MouseEvent.CLICK, mouseClick); 
} 

回答

2

保留对按钮的引用并将它们添加到数组中。

var agreeButton:Button = new Button(); 
var disagreeButton:Button = new Button(); 

//... Code that will add the above instantiated buttons to the canvas 

var buttonArray:Array = new Array(agreeButton, disagreeButton); 

for (var i:int = 0; i < buttonArray.length; i++) {  
buttonArray[i].addEventListener(MouseEvent.CLICK, mouseClick); 
} 


private function mouseClick(event:MouseEvent):void { 
    Alert.show("Boom!"); 
} 
+0

的简单例子,非常感谢! – Anderson 2012-04-11 16:05:32

+0

只是一个简单的问题,那么“container.clickButton1”,“container.clickButton2”按钮呢?他们在名为“容器”的动画片段中。你如何在动画片段中声明变量? – Anderson 2012-04-11 16:08:47

+0

另外我用SimpleButton代替。 – Anderson 2012-04-11 16:10:46

1

我会做一个button类,每个这些按钮扩展。在按钮类中添加eventListeners

例子:

原谅我,如果有事情不对的,我没有在AS3编码了一段时间。

class MyButton extends Button 
{ 
    public function MyButton() 
    { 
     this.addEventListener(MouseEvent.ROLL_OVER, mouseRollOver); 
     this.addEventListener(MouseEvent.ROLL_OUT, mouseRollOut); 
     this.addEventListener(MouseEvent.CLICK, mouseClick); 
    } 
    //... Add mouseRollOver, mouseRollOut, mouseClick methods 
} 

你各个按钮

class DisagreeButton extends MyButton 
{ 
    public function DisagreeButton() 
    { 

    } 
} 

class AgreeButton extends MyButton 
{ 
    public function AgreeButton() 
    { 

    } 
} 

一旦你实例化所有的事件监听器将被应用到每个按钮。

+0

你能举个例子吗? – Anderson 2012-04-11 15:55:25

+0

@Anderson我加了一个我所指的 – brenjt 2012-04-11 16:42:03