2011-11-02 71 views
1

我不知道是否對我的問題addChild和removeChild方法是我確切需要的,但當我在網上閱讀這是我需要的技術。Actionscript3.0 addChild removeChild XML

這是我的XML文件:

<projects> 
    <category name = "Branding"> 
     <project>Logo e effekt Medias1</project> 
     <project>Portali Logo1</project> 
     <project>Skena Logo1</project> 
    </category> 
    <category name = "Web"> 
     <project>Logo e effekt Medias2</project> 
     <project>Portali Logo2</project> 
     <project>Skena Logo2</project> 
    </category> 
    <category name = "Print"> 
     <project>Logo e effekt Medias3</project> 
     <project>Portali Logo3</project> 
     <project>Skena Logo3</project> 
    </category> 

從這個XML文件,首先是我創建從類別名稱屬性,並用菜單中的for循環我創建這個菜單和它看起來是這樣的:

品牌 網絡 打印

後,當我點擊讓說我品牌推廣創建另一個動畫片段,並在該動畫片段上顯示品牌下的所有項目 。直到這裏一切都很好。

當我點擊其他菜單時出現問題。比方說,我點擊了網頁,此時所有品牌項目都停留在舞臺上,並加入所有在網絡展示中的項目。這種方式我一直點擊菜單上的新數據出現。

這是最好用的技術,所以當我點擊菜單時,只有與該類別相關的數據纔會出現在舞臺上。

這裏有鏈接,如果你想檢查我的爛攤子:LINK

回答

1

嗯,這不是真正的添加或刪除的兒童。真正的問題是關於你的發展觀點。

首先,你正在建立一個菜單,所以讓菜單就是這樣,一個對象發送信號來通知哪個按鈕被點擊。

之後,您需要某種形式的容器,當收到菜單消息時將顯示相關屏幕。

此容器可以包含三個屏幕,品牌,Web &打印。

您可以將所有這些屏幕可見屬性設置爲false。

當容器接收到菜單信息時,它將所選屏幕可見屬性設置爲true,其他屏幕設置爲false。

這樣,您不必繼續添加或刪除子項,試圖跟蹤您的DisplayList結構。

使用菜單中的事件調度。

不管怎麼說,這是一個粗略的例子,它不是完整的,但應該給你一個想法......

public class Menu extends Sprite 
{ 
    //A basic button instance 
    private var button:Sprite = new Sprite(); 

    //The dispatcher dispatches & listen to events 
    //it acts as a connector between your container 
    //and the menu 
    private var dispatcher:EventDispatcher; 

    //The menu will be created in the Container 
    //the dispatcher will be passed as a parameter 
    //this is the connection between the two classes 
    //Please note that there are other ways to achieve 
    //a similar result.. 
    public function Menu (dispatcher:EventDispatcher) 
    { 
     //assign the dispatcher instantiated in the container 
     //to a variable in order to manipulate it in this class 
     this.dispatcher = dispatcher; 

     //takes care of creating the menu 
     createMenu(); 
    } 

    private function clickHandler(event:MouseEvent):void 
    { 
     //each time a button is click an event is dispatched 
     //that contains the name of the clicked button 
     dispatcher.dispatchEvent 
       (new MenuEvent(event.currentTarget.name)); 
    } 

    private function createMenu():void 
    { 
     //here you load the XML, create the buttons 
     // and add the event listeners 

     //this is just an example for the logic 
     //it's irrelevant since the button will 
     //be created from the XML 
     button.name = "Branding"; 
      addChild(button); 
      button.addEventListener 
       (MouseEvent.CLICK , clickHandler); 
    } 
} 


public class Container extends Sprite 
{ 
    private var button:Sprite = new Sprite(); 

    //Here we instantiate a dispatcher 
    private var dispatcher:EventDispatcher = new EventDispatcher; 
    private var menu:Menu; 

    //a basic screen instance that could contain 
    //all that has to do with Branding for instance 
    private var screen1:Sprite = new Sprite(); 
    //etc... 

    public function Container() 
    { 
     //The dispatcher is set to listen to the events 
     //dispatched in the Menu class 
     dispatcher.addEventListener(MenuEvent.MENU , eventListener); 
     screen1.visible = false; 

     //now the menu can be created 
     menu = new Menu(dispatcher); 
     addChild(menu); 
    } 

    private function eventListener(event:MenuEvent):void 
    { 
     //set all the screens visibility to false 
     //here... 

     //get the event name and react accordingly 
     //here you can implement a switch 

     switch(event.name) 
     { 
      case "Branding": 
      //show Branding screen 
      screen1.visible = true; 
      break; 

      //etc... 

     }         
    } 
} 

public class MenuEvent extends Event 
{ 
    public const MENU:String = "Menu Event"; 
    public var name:String; 

    //Check custom events for the rest of the code... 
    //Do a Google search for custom events 
    //shouldn't be too difficult to find 
} 
+0

感謝帕特里克我會檢討你的解決方案,我會回來給你。第一視圖看起來很好! – AXheladini

+0

但我的菜單是動態的,我沒有菜單上只有三個類別。它是動態的,我將需要添加更多的類別,並改變他們的時間,這就是爲什麼我使用XML! – AXheladini

+0

沒關係......我只是給了你基本的邏輯。您可以使用與XML相同的原則。這並不重要如何創建按鈕 – PatrickS