2012-12-02 56 views
0

即時通訊工作在一個Flex應用程序,我需要動態更新按鈕圖標源,但是,它不足以通過在運行時將圖標屬性更改爲另一個類變量,我需要明確更改給另一個類的源。我谷歌我的懷疑,但沒有答案呢。動態更改嵌入源類

我想要的東西,像下面這樣: http://www.java2s.com/Code/Flex/Graphics/ChangeImagesourceinbuttonaction.htm

但我需要做的somethig這樣,而不是:

[Embed(source="sun.jpg")] 
[Bindable] 
private var dayAsset:Class; 

private function init():void { 
    dayImage.source = dayAsset; 
} 

private function showMoon():void { 
    dayAsset.source = "moon.jpg"; 
} 

private function showSun():void { 
    dayAsset.source = "sun.jpg"; 
} 

我曾嘗試沒有成功之前的代碼。

爲什麼我需要以這種方式更新「dayImage」圖片源?因爲我有多個位置參考的圖像,所以我需要在事件觸發時對其進行更新。

任何解決方案:P或評論將不勝感激。

謝謝。有一個愉快的夜晚。

+0

我不繼。如果你想更新dayImage.source爲什麼你不能只是'dayImage.source = newValue'?如果你要改變enbed,那麼你不能。嵌入是在編譯時執行的,您不能在編譯時更改它們。 – JeffryHouser

+0

是的,我不知道我無法改變嵌入,但事情是,我有不止一個圖像指的是嵌入,所以,如果我嘗試瞭如:'dayImage.source = newValue',我是將被迫爲所有圖像做 –

回答

0

如果我理解這個問題;那麼答案就是你不能在運行時改變嵌入。它們在編譯時執行;併成爲編譯後的SWF的一部分。

您很可能希望做一個像這樣的方法:

// embed both images 
[Embed(source="sun.jpg")] 
[Bindable] 
private var dayAssetSun:Class; 

[Embed(source="moon.jpg")] 
[Bindable] 
private var dayAssetMoon:Class; 

// us a variable to store the reference to the image you want to see 
private var currentDayAsset :Class; 

// set the current asset 
private function init():void { 
    dayImage.source = currentDayAsset; 
} 

// these methods change the currentDayAsset variable; but do not affect the embeds 
private function showMoon():void { 
    currentDayAsset = dayAssetMoon; 
} 

private function showSun():void { 
    currentDayAsset = dayAssetSun; 
} 
+0

好的男人,這是我想要的功能,我不知道嵌入式不能在運行時分配。非常感謝。 –

+0

我必須對答案發表評論,除了必須將可綁定標記應用於「dayAssetMoon」變量之外,這一切都是正確的。這是因爲「currentDayAsset」是將在所有圖像上作爲源引用的變量,所以它是需要可綁定的變量。 –

+1

@HugoAllexisCardona currentDayAsset是否需要綁定或不能真正取決於你如何使用它;這不是你的問題(或我的答案)所涵蓋的內容。但是,如果您在MXML中使用它作爲Image標籤之類的來源;那麼是的,你需要使'currentDayAsset'可綁定。 – JeffryHouser