2015-04-28 58 views
1

下面的代碼從XML列表中帶回9個攝影子實例。然後我想要做的是,當用戶點擊其中一個返回值時,它將刪除所有攝影值並加載具有該孩子圖像的用戶。即當單擊運動時,將會返回安全提示,並返回XML文件中的任何其他名稱。操作xml數據actionscript

 var list:XMLList = xmlinfo.profile.photography; 

     var totalimage:Number = list.length(); 
     trace("length" + totalimage); 

     for (var i:int =0; i<totalimage; i++){ 
      trace(xmlinfo.profile.photography[i]); 


    //bkg.addEventListener(MouseEvent.CLICK,gotodata); 
     background = new bkg(); 


     background.y = i*40; 
     background.x = 20; 
     addChild(background); 

     textField = new TextField(); 
     textField.text = list[i];  
     background.addChild(textField); 
      } 
     } 

XML文件

<root> 
    <profile> 
     <name>ann lee</name> 
     <photography>sport</photography> 
     <photography>landscape</photography> 
     <photography>still life</photography>   
     <image>img1.jpg</image> 
    </profile> <profile>   
     <name>john</name> 
     <photography>wildlife</photography> 
     <photography>landscape</photography> 
     <image>img2.jpg</image> </profile> 
</root> 
+0

你的代碼看起來還好。它不是做你想做的事嗎?有幾種方法可以改進它,但它會做同樣的事情。不太清楚你的問題實際上是什麼。 – Aaron

+0

@ user3658394感謝您的澄清。每個問題只能提出一個問題。不要將所有內容添加到評論中,請編輯您的問題。 – null

+0

嗨亞倫。也許我沒有把我的問題弄清楚。爲了更加清晰,我編輯了上面的文字。我會接受你的建議:) – caribou

回答

1

這不是有效的XML:

<root> 
<profile> 
    <name>ann lee</name> 
    <photography>sport</photography> 
    <photography>landscape</photography> 
    <photography>still life</photography>   
    <image>img1.jpg</image> 
<profile> 
    <name>john</name> 
    <photography>wildlife</photography> 
    <photography>landscape</photography> 
    <image>img2.jpg</image> 
</root> 

輪廓標籤永遠不會關閉。

關於你的代碼中的本地變量應該使用括號:

var background:bkg; //you should use names with better meaning and use capital letters for class names (compare to the next line, example: var background:Background; 
var textField:TextField; 

for (var i:int =0; i<totalimage; i++){ 
    trace(xmlinfo.profile.photography[i]); 


    background.addEventListener(MouseEvent.CLICK,gotodata); 

    background = new bkg(); 
    background.y = i*40; 
    background.x = 20; 
    addChild(background); 

    textField = new TextField(); 
    textField.text = list[i];  
    background.addChild(textField); 

    } 
} 
+0

感謝您的建議。我的XML是正確的,我發佈時錯誤地刪除了結束標籤。不是常規的AS用戶,我確實有一些不好的做法,但我會聽取您的建議並嘗試在未來實施它們。如果您提供更多建議,我已在上述帖子中澄清了我的問題。 – caribou