AS3 - 带有文本字段的菜单
AS3 - Menu with textfields
我想不出一个更好的标题名称,如果不是那么明确,我很抱歉。我正在尝试制作一个菜单(用于我的服务器程序),每次客户端连接到它时都会添加一个带有 ip 的文本框,并在客户端断开连接时删除客户端的文本框。
无人连接时:
当人们连接时:
最大的问题是我希望断开连接的 IP 下方的所有其他 IP 都向上移动。我如何在 AS3 中做到这一点?
删除 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.
希望能帮到你。
如果您不想移动所有这些,只需保留您的 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];
}
我想不出一个更好的标题名称,如果不是那么明确,我很抱歉。我正在尝试制作一个菜单(用于我的服务器程序),每次客户端连接到它时都会添加一个带有 ip 的文本框,并在客户端断开连接时删除客户端的文本框。
无人连接时:
当人们连接时:
最大的问题是我希望断开连接的 IP 下方的所有其他 IP 都向上移动。我如何在 AS3 中做到这一点?
删除 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.
希望能帮到你。
如果您不想移动所有这些,只需保留您的 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];
}