我无法想出一个更好的名称,我很抱歉,如果不是那个声明。我正在尝试创建一个菜单(对于我的服务器程序),每次客户端连接时都会添加带有ip的文本框,并在客户端断开连接时删除客户端的文本框。
当没有人连接时:
当人们联系时:
最大的问题是我希望所有其他ip都低于断开连接的ip。我怎么能在AS3中做到这一点?
答案 0 :(得分:1)
删除TextField
后,您可以重新定位所有其他文本字段:
// 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++;
}
}
}
当然,这段代码可以显示出如何做你想做的事情,你应该改进它并根据你的需要进行调整。您可以使用此代码here。
希望所有可以帮到你的事。
答案 1 :(得分:1)
如果您不想移动所有这些内容,只需保留TextFields
的列表,然后当您断开连接时,找到索引,然后移动所有后续的索引。类似的东西:
private var m_textFields:Vector.<TextField> = new Vector.<TextField>(); // all our textfields
private var m_ipToTextField:Object = new Object; // an object to make it easier to find our textfield
public function addIP( ip:String ):void
{
var tf:TextField = this._createTextField( ip ); // create, style, and add your textfield
tf.y = tf.height * this.m_textFields.height; // assuming they're all the same height
// add it to our storage objects
this.m_textFields.push( tf );
this.m_ipToTextField[ip] = tf;
}
public function removeIP( ip:String ):void
{
// get our textfield and remove it
var tf:TextField = this.m_ipToTextFields[ip];
tf.parent.removeChild( tf );
// get our index of the textfield in the vector
var index:int = this.m_textFields.indexOf( tf );
// move all subsequent textfields up by the height of the textfield that we removed
var len:int = this.m_textFields.length;
var h:Number = tf.height;
for( var i:int = index + 1; i < len; i++ )
this.m_textFields[i].y -= h;
// remove the textfield from our storage objects
this.m_textFields.splice( index, 1 );
this.m_ipToTextField[ip] = null;
delete this.m_ipToTextField[ip];
}