2009-02-17 76 views
3

看看這個代碼:爲什麼我無法在Flash CS4的回調方法中訪問組合框?

import mx.core.View; 
var accordianPane = my_acc.createSegment("mcElectrical", "panel0", "Electrical", "payIcon"); 
accordianPane.comboBox.addItem("test"); 

這增加了一個項目與標籤「測試」在影片剪輯的組合框。它工作得很好。但是,當我在回調函數中放入相同的代碼時,它會失敗。

import mx.core.View; 

// Load Cost Guide feed. 
var costGuideUrl:String = "http://test/cost-guide.ashx"; 
var costGuideXml:XML = new XML(); 
costGuideXml.onLoad = function(success) { 
    if(success) { 
     populateAccordian(this); 
    } else { 
     // Display error message. 
    } 
}; 
costGuideXml.load(costGuideUrl); 

// Populate Accordian with retrieved XML. 
function populateAccordian(costGuideXml:XML) { 

    var accordianPane = my_acc.createSegment("mcElectrical", "panel0", "Electrical", "payIcon"); 
    accordianPane.comboBox.addItem("test"); 
    // This line definitely executes... 
} 

任何想法,爲什麼我無法從回調內到達組合框?

回答

2

好,所以首先看起來喲使用AS2。

因爲它是as2,所以問題可能是一個範圍問題。範圍界定在as2與as3中的工作方式不同。將我的思緒推回到我的as2天,當您設置此回調函數時,您便處於costGuideXML的範圍內。由於您處於此範圍內,因此您無權訪問my_acc變量。

您可能需要使用的是Delegate類,以使populateAccordian方法在原始類的範圍內執行(如果這是在時間軸上,則有可能是動畫片段)。

類似的信息(雖然這是未經測試):

import mx.utils.Delegate; 

    // Load Cost Guide feed. 
    var costGuideUrl:String = "http://test/cost-guide.ashx"; 
    var costGuideXml:XML = new XML(); 
    costGuideXml.onLoad = Delegate.create(this, xmlLoadedHandler); 
    costGuideXml.load(costGuideUrl); 

    function xmlLoadedHandler() : Void 
    { 
    populateAccordian(costGuideXml); 
    } 

    // Populate Accordian with retrieved XML. 
    function populateAccordian(costGuideXml:XML) { 

     var accordianPane = my_acc.createSegment("mcElectrical", "panel0", "Electrical", "payIcon"); 
     accordianPane.comboBox.addItem("test"); 
     // This line definitely executes... 
    } 
相關問題