【问题标题】:AS3 Loop Button (Add listener and function)AS3 循环按钮(添加监听器和函数)
【发布时间】:2013-12-20 15:03:31
【问题描述】:

我将如何进行循环,这样我就不必复制和粘贴相同的代码,最多可达 10 个或更高?

optionsmenu.char01.addEventListener(MouseEvent.CLICK, gochar01);
function gochar1 (event:MouseEvent): void {
    char.gotoAndStop(1);
}
optionsmenu.char02.addEventListener(MouseEvent.CLICK, gochar02);
function gochar2 (event:MouseEvent): void {
    char.gotoAndStop(2);
}

【问题讨论】:

    标签: actionscript-3 loops button listener


    【解决方案1】:

    你可以试试这样的:

    var callbackGenerator:Function = function(i:int):Function {
      return function(event:MouseEvent):void {
        char.gotoAndStop(i);
      };
    };
    
    // Change this according to size of your menu
    var menuSize:int = 12;
    
    for (var i:int = 1; i < menuSize; i++) {
      // Prefix with 0
      var index:String = i < 10 ? '0' + i : String(i);
    
      // Generate the click callback
      var callback:Function = callbackGenerator(i);
    
      // Add the click event listener
      optionsmenu['char' + index].addEventListener(MouseEvent.CLICK, callback);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-30
      • 2011-03-02
      • 2013-12-24
      • 2014-04-11
      • 2012-10-10
      • 2012-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多