2012-04-11 72 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