2012-04-11 102 views
1

我该如何做这项工作?我需要从引用中删除单词Button来引用将被着色的实际项目。先谢谢你。AS3参考按钮

disagreeButton.addEventListener(MouseEvent.ROLL_OVER, mouseRollOver); 
contentMain.clickButton1.addEventListener(MouseEvent.ROLL_OVER, mouseRollOver); 

function mouseRollOver(e:MouseEvent):void { 
    var c:Color = new Color(); 
    c.setTint (0xFFFFFF, 1); 
    //the issue is this line: 
    this[e.target.name.replace("Button", "")].transform.colorTransform = c; 
} 
+1

什么是错的这个解决方案?它是否编译?跑?有意想不到的副作用? – 2012-04-11 17:35:39

+0

TypeError:错误#1010:术语未定义且没有属性。 \t at index_test_fla :: MainTimeline/mouseRollOver() 只为“contentMain.clickButton1”得到这个错误 – Anderson 2012-04-11 17:48:35

+0

您是否测试了e.target.name.replace(“Button”,“”)'评估您的想法它呢? – 2012-04-11 17:49:31

回答

1

现在,我明白你的问题,我会推荐以下内容。

  1. 有按钮单独方法this,并在contentMain,任何人。然后在适当情况下通过this[...]contentMain[...]访问对象。

    disagreeButton.addEventListener(MouseEvent.ROLL_OVER, mainMouseRollOver); 
    contentMain.clickButton1.addEventListener(MouseEvent.ROLL_OVER, contentMainMouseRollOver); 
    
    function mainMouseRollOver(e:MouseEvent):void { 
        mouseRolloverLogic(this[e.target.name.replace("Button", "")]; 
    } 
    
    function contentMainMouseRollOver(e:MouseEvent):void { 
        mouseRolloverLogic(contentMain[e.target.name.replace("Button", "")]; 
    } 
    
    function mouseRolloverLogic(item:DisplayObject):void { 
        var c:Color = new Color(); 
        c.setTint (0xFFFFFF, 1); 
        item.transform.colorTransform = c; 
    } 
    
  2. 为您拥有的方法添加额外的逻辑。

    if (this[...] != null) { 
        ... 
    }else if (contentMain[...] != null) { 
        ... 
    } 
    
+0

谢谢你这个作品。有没有“干净”的方式来做到这一点或没有? – Anderson 2012-04-11 18:02:03

+0

@Anderson第一种选择可能被认为是“更清洁”。只需为主应用中的contentMain和Buttons中的按钮创建不同的'mouseRollOver'函数。 – 2012-04-11 18:03:20

+0

似乎很奇怪重复一个功能,只是因为目标的差异。我只是觉得在主时间线上有更有效的方法来获得所有的代码。 – Anderson 2012-04-11 18:05:39