1

私の問題で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 属性からメニューを作成し、for ループを使用してこのメ​​ニューを作成すると、次のようになります。

ブランディングウェブプリント

その後、ブランディングをクリックすると、別のムービークリップが作成され、そのムービークリップにブランディングの下のすべてのプロジェクトが表示されます。ここまではすべて問題ありません。

他のメニューをクリックすると問題が発生します。Web をクリックするとします。この時点で、Branding のすべてのプロジェクトがステージに表示され、Web の下にあるすべてのプロジェクトがステージに表示されます。このようにして、メニューをクリックするたびに新しいデータが表示されます。

メニューをクリックすると、そのカテゴリに関連するデータのみがステージに表示されます。

ここに私の混乱を確認したい場合のリンクがあります: LINK

4

1 に答える 1

1

ええと、それは実際には子を追加または削除することではありません。本当の問題は、開発の視点に関するものです。

まず第一に、メニューを構築しているので、どのボタンがクリックされたかを通知する信号を送るオブジェクトであるメニューにしましょう。

その後、メニューのメッセージを受け取ったときに関連する画面を表示する何らかの形のコンテナが必要です。

このコンテナには、Branding、Web & Print の 3 つの screen を含めることができます。

このすべての画面の表示可能なプロパティを 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
}
于 2011-11-02T16:06:54.370 に答える