【问题标题】:Actionscript3.0 addChild removeChild XMLActionscript3.0 addChild removeChild XML
【发布时间】:2011-12-20 09:41:27
【问题描述】:

我不知道对于我的问题 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 文件中,我从类别的名称属性创建一个菜单,并使用 for 循环创建这个菜单,它看起来像这样:

品牌 网络 打印

之后,当我单击让我们说品牌时,我创建了另一个影片剪辑并在该影片剪辑上 显示品牌下的所有项目。到这里一切正常。

当我单击其他菜单时出现问题。假设我点击Web,此时Branding 的所有项目都停留在舞台上,web 下的所有项目都显示在舞台上。这样我每次点击菜单都会出现新数据。

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

如果你想查看我的烂摊子,这里有链接:LINK

【问题讨论】:

    标签: xml flash actionscript-3 removechild addchild


    【解决方案1】:

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

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

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

    此容器可以包含三个屏幕,品牌、网络和打印。

    您可以将所有此屏幕可见属性设置为 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
    }
    

    【讨论】:

    • 感谢 patrick,我会审核您的解决方案,我会尽快回复您。第一次看还不错!
    • 但是我的菜单是动态的,我的菜单上不只有三个类别。它是动态的,我需要添加更多类别并到时候更改它们,这就是我使用 xml 的原因!
    • 没关系...我只是给了你基本的逻辑。您可以对 XML 使用相同的原则。按钮的创建方式无关紧要
    • Patrick 您输入的代码非常专业,我现在还没有达到这个水平。即使我理解了逻辑,我仍然不知道哪个功能是做什么用的?如果您能够评论每个功能,因为它们的功能会很好!
    猜你喜欢
    • 1970-01-01
    • 2012-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多