Search code examples
actionscript-3flashactionscript

AS3 - Menu with textfields


I could not come up with a better name for the title, I am sorry if it is not that declarative. I am trying to make a menu (for my server program) that adds a textbox with the ip each time a client connects to it, and remove the textbox of the client when the client disconnects.

When nobody has connected: when nobody has connected

When people has connected: when people have connected

The big problem is that I want all the other ip's below the one that disconnected to move up. How can I do that in AS3?


Solution

  • After removing your TextField, you can re-position all other text fields like this :

    // the menu items container
    var menu:MovieClip = new MovieClip()
        addChild(menu);
    
    for(var i=0; i<5; i++){
        var txt:TextField = new TextField();
            txt.x = 20;
            txt.y = 26*i + 20;
            txt.height = 24;
            txt.width = 120;
            txt.text = 'client : ' + i.toString();
            txt.border = true;
            txt.addEventListener(
                MouseEvent.CLICK,
                function(e:MouseEvent):void {               
                    var parent:DisplayObjectContainer = e.target.parent; // which is the "menu" movieclip here
                    e.target.parent.removeChild(e.target);
                    set_objects(parent);        
                }
            )
        menu.addChild(txt)
    }
    
    function set_objects(container:DisplayObjectContainer){ 
    
        var j:int = 0;
        for(var i:int = 0; i < container.numChildren; i++){
            var child:DisplayObject = container.getChildAt(i);
    
            // if the child is a TextFiled, set its new position
            if(child is TextField){         
                child.y = 26*j + 20;
                j++;
            }       
        }   
    }
    

    Of course, this code is jut to show a manner how to do what you want, you should improve it and adapt it to your needs. You can this code working here.

    Hope all that can help you.