Actionscript3.0 addChild removeChild XML

时间:2011-11-02 14:47:46

标签: xml flash actionscript-3 removechild addchild

我不知道对于我的问题addChild和removeChild方法是我真正需要的,但是当我在网上阅读时,我需要使用的技术。

这是我的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文件首先我要从类别的name属性创建一个Menu,并使用for循环创建此菜单,它看起来像这样:

品牌 卷筒纸 打印

之后,当我点击时,让我说品牌我创建另一个动画片段和该动画片段 显示品牌下的所有项目。到这里一切都还好。

单击其他菜单时出现问题。假设我点击了Web,此时所有来自Branding的项目都会停留在舞台上,加上舞台上的所有网络显示项目。这种方式我一直点击菜单上出现的新数据。

这是最好的技术,当我点击菜单时,只有与该类别相关的数据才会出现在舞台上。

如果你想检查我的混乱,你可以在这里找到链接:LINK

1 个答案:

答案 0 :(得分:1)

嗯,这不是关于添加或删除孩子的。真正的问题是关于你的发展前景。

首先,你正在构建一个菜单,所以让菜单就是这样,一个发送信号的对象,告知点击了哪个按钮。

之后,您需要某种形式的容器,在收到菜单消息时会显示相关屏幕。

这个容器可以包含三个屏幕,Branding,Web&amp;打印。

您可以将所有此屏幕的可见属性设置为false。

当容器收到菜单信息时,它会将所选屏幕的visible属性设置为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
}