2009-11-04 29 views
0

我試圖在動態生成的導航「節點」被點擊時傳遞變量的當前值。這隻需要一個整數,但它總是導致最後一個節點的值..已經嘗試了一些不同的方法來傳遞值,一個自定義事件監聽器,一個setter,但我懷疑這是一個封閉的問題..幫助將不勝感激;-)關閉問題? - 傳遞變量的當前值

function callGrid():void { 

    for (var i:Number = 0; i < my_total; i++) { 

     var gridnode_url = my_grid[i][email protected]; 
     var news_category= my_grid[i][email protected]; 
     var newstitle = my_grid[i][email protected]; 
     var news_content = my_grid[i]..news_content; 
     var news_image = my_grid[i]..news_image; 

     var gridnode_loader = new Loader(); 
     container_mc.addChild(gridnode_loader); 
     container_mc.mouseChildren = false; 
     gridnode_loader.load(new URLRequest(gridnode_url)); 
     gridnode_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, gridLoaded); 
     gridnode_loader.name = i; 

     text_container_mc = new MovieClip(); 
     text_container_mc.x = 0; 
     text_container_mc.mouseEnabled = false; 
     var textY = text_container_mc.y = (my_gridnode_height+18)*y_counter; 
     addChild(text_container_mc); 
     var tf:TextSplash=new TextSplash(newstitle,10,0,4); 
     container_mc.addChild(tf); 
     tf.mouseEnabled = false; 
     tf.height = my_gridnode_height; 
     text_container_mc.addChild(tf); 
     var text_container_mc_tween = new Tween(text_container_mc, "alpha", Strong.easeIn, 0,1,0.1, true); 

     gridnode_loader.x = (my_gridnode_width+5) * x_counter; 
     gridnode_loader.y = (my_gridnode_height+15) * y_counter; 

     if (x_counter+1 < columns) { 
      x_counter++; 
     } else { 
      x_counter = 0; 
      y_counter++; 
     } 
    } 
} 
function gridLoaded(e:Event):void { 
    var i:uint; 
    var my_gridnode:Loader = Loader(e.target.loader); 
    container_mc.addChild(my_gridnode); 
    _xmlnewstarget = my_gridnode.name; 

//|||||||||||||||||||||||||||||||||||||||| 
//when a particular grid node is clicked I need to send the current _xmlnewstarget value to the LoadNewsContent function... 
//||||||||||||| |||||||||||||||||||||||| 

    my_tweens[Number(my_gridnode.name)]=new Tween(my_gridnode, "alpha", Strong.easeIn, 0,1,0.1, true); 
    my_gridnode.contentLoaderInfo.removeEventListener(Event.COMPLETE, gridLoaded); 
    my_gridnode.addEventListener(MouseEvent.CLICK, loadNewsContent); 

} 

function loadNewsContent(e:MouseEvent):void { 
    createNewsContainer(); 
    getXMLNewsTarget(); 
    news_category = my_grid[_xmlnewstarget][email protected]; 
    var tfnews_category:TextSplash=new TextSplash(news_category,20,16,32,false,false,0xffffff); 
    tfnews_category.mouseEnabled = false; 

    newstitle = my_grid[_xmlnewstarget][email protected]; 
    var tftitle:TextSplash=new TextSplash(newstitle,20,70,24,false,false,0x333333); 
    news_container_mc.addChild(tftitle); 
    tftitle.mouseEnabled = false; 

    news_content = my_grid[_xmlnewstarget]..news_content; 
    var tfnews_content:TextSplash=new TextSplash(news_content,20,110,20,true,true,0x333333,330); 
    news_container_mc.addChild(tfnews_content); 
    tfnews_content.mouseEnabled = false; 
    news_image = my_grid[_xmlnewstarget][email protected]_image; 
    loadNewsImage(); 
    addChild(tfnews_category); 
    addChild(tftitle); 
    addChild(tfnews_content); 

    var news_container_mc_tween = new Tween(news_container_mc, "alpha", Strong.easeIn, 0,1,0.3, true); 
    news_container_mc_tween.addEventListener(Event.INIT, newsContentLoaded); 
} 

回答

5

我不會嘗試讀取你的代碼(嘗試在你的格式化工作,即使它只是縮進),但我會提供一個簡單的例子:

for (var i = 0; i < my_total; i++) { 
    var closure = function() { 
     // use i here 
    } 
} 

正如你所說,當調用closure時,它將包含最後一個值i(在這種情況下將是my_total)。做到這一點,而不是:

for (var i = 0; i < my_total; i++) { 
    (function(i) { 
     var closure = function() { 
      // use i here 
     } 
    })(i); 
} 

這將創建其「捕獲」的i的電流值,使您可以關閉指該值的環內的另一個功能。

另請參見How does the (function() {})() construct work and why do people use it?瞭解更多相似的例子。

+0

所以總結:)範圍*而不是*變量,在Javascript中被「封閉」,只有一個新的函數聲明可以創建一個新的範圍。 – 2009-11-04 01:44:21

+0

感謝格雷格關閉功能,這真的有幫助。我添加了代碼,確保類型轉換是正確的,併爲_xmlnewstarget變量添加了一個setter。我創造了完美的輪盤! 5個節點24130,01432,04312,02143,02143,14302,02143,21403,43021,24310,02143,14302,02143,43021 ...的最後值是_xmlnewstarget的最後一個值... – martin 2009-11-04 23:28:28

+0

我設法通過一個不同的路線最終對此進行排序..我在xml中爲每個新聞項添加了一個新聞標識,並將其引用到文本容器中 - text_container_mc.id = my_grid [i]。@ id;然後我直接用text_container_mc.addEventListener(MouseEvent.CLICK,loadNewsContent)跳轉到loadNewsContent;然後在該函數中使用e.target.id鏈接文本字段 - newstitle = my_grid [e.target.id]。@ newstitle; 現在我只需要整理反向導航..但我認爲這個問題是排序;-)謝謝 – martin 2009-11-05 12:31:51

0

嗯,如上所述,代碼是有點密集的,但我認爲你可能有點類型轉換問題之間的字符串和整數,是「最後值」總是0?嘗試做出這些改變,讓我知道你如何繼續。

// replace this gridnode_loader.name = i; 
gridnode_loader.name = i.toString(); 

// explictly type this as an int 
_xmlnewstarget = parseInt(my_gridnode.name); 

// replace this: my_tweens[Number(my_gridnode.name)] = new Tween(...... 
my_tweens[parseInt(my_gridnode.name)] = new Tween(); 

哦,我想這不用說,你應該大規模重構這個代碼塊一旦你得到了它的工作。

編輯:經過進一步的研究,我認爲你需要這個

//replace this: my_gridnode.addEventListener(MouseEvent.CLICK, loadNewsContent); 

var anonHandler:Function = function(e:MouseEvent):void 
{ 
    loadNewsContent(_xmlnewstarget); 

}; 
my_gridnode.addEventListener(MouseEvent.CLICK, anonHandler); 

如果您loadNewsContent改變arguements從(e:MouseEvent)(id:String)

+0

對於代碼佈局感到抱歉,我會嘗試儘快併入您的建議;-) – martin 2009-11-04 23:30:54

0

首先,你並不需要調用的addChild爲同一裝載機兩次(一次在callGrid中),然後在(gridLoaded)中。然後你可以嘗試把內部加載新聞內容:
news_category = my_grid[int(e.target.name)][email protected];
而不是
news_category = my_grid[_xmlnewstarget][email protected];
由於_xmlnewstarget似乎是更大的範圍,這就是爲什麼它每次加載操作完成時得到更新。

+0

謝謝你,我會盡快做一些測試 – martin 2009-11-04 23:31:32