0

我在应用程序中有一个按钮,用户可以单击该按钮来添加多个输入字段。函数如下。

private var fieldHolder:Sprite = new Sprite();

private function addInviteFriend(event:starling.events.Event):void
{
    this.newFriendInvite = new TextInput();
    this.newFriendInvite.backgroundSkin = new Quad(Constants.STAGE_WIDTH - 80, 30, 0xd0d0d1);
    this.newFriendInvite.width = Constants.STAGE_WIDTH - 110;
    this.newFriendInvite.height = 30;
    this.newFriendInvite.text = "";
    this.fieldHolder.addChild(this.newFriendInvite);
    this.newFriendInvite.x = 0;
    this.newFriendInvite.y = this.fieldHolder.height;

    this.removeInvitedFriend = new Button();
    this.removeInvitedFriend.defaultSkin = new Image(Assets.getAtlasTexture("btn-delete"));
    this.fieldHolder.addChild(this.removeInvitedFriend);
    this.removeInvitedFriend.x = int((this.newFriendInvite.x + this.newFriendInvite.width) - this.newFriendInvite.defaultSkin.width);
    this.removeInvitedFriend.y = int(this.newFriendInvite.y);
    this.removeInvitedFriend.addEventListener(starling.events.Event.TRIGGERED, removeInviteFriendClick);

    this.addInviteFriendButton.y = this.fieldHolder.y + this.fieldHolder.height + 30 + 1;
}

private function removeInviteFriendClick(event:starling.events.Event):void
{
    var child:Sprite = event.currentTarget as Sprite;       
    this.fieldHolder.removeChild(child);
}

一切都很好,我可以添加多个输入字段但是问题是当我单击删除按钮时,只有按钮本身被删除,而不是文本输入字段。我知道为什么只有按钮被删除,但我不知道如何在 removeChild 进程中包含文本输入。

我尝试创建另一个 Sprite 来保存 fieldHolder 的所有实例,但这没有用。我也尝试了 removeChildren ,但这也没有用。

谢谢

4

1 回答 1

0

尝试将这两个元素添加到容器中然后调用removeChildren它?

private var fieldHolder:Sprite = new Sprite();

private function addInviteFriend(event:starling.events.Event):void
{
    var container:Sprite = new Sprite();

    this.newFriendInvite = new TextInput();
    this.newFriendInvite.backgroundSkin = new Quad(Constants.STAGE_WIDTH - 80, 30, 0xd0d0d1);
    this.newFriendInvite.width = Constants.STAGE_WIDTH - 110;
    this.newFriendInvite.height = 30;
    this.newFriendInvite.text = "";
    this.newFriendInvite.x = 0;
    this.newFriendInvite.y = this.fieldHolder.height;
    container.addChild(this.newFriendInvite);

    this.removeInvitedFriend = new Button();
    this.removeInvitedFriend.defaultSkin = new Image(Assets.getAtlasTexture("btn-delete"));
    this.removeInvitedFriend.x = int((this.newFriendInvite.x + this.newFriendInvite.width) - this.newFriendInvite.defaultSkin.width);
    this.removeInvitedFriend.y = int(this.newFriendInvite.y);
    this.removeInvitedFriend.addEventListener(starling.events.Event.TRIGGERED, removeInviteFriendClick);
    container.addChild(this.removeInvitedFriend);

    this.addInviteFriendButton.y = this.fieldHolder.y + this.fieldHolder.height + 30 + 1;

    this.fieldHolder.addChild(container);
}

private function removeInviteFriendClick(event:starling.events.Event):void
{
    var child:Sprite = event.currentTarget as Sprite;       
    child.parent.removeChildren();
}
于 2013-07-20T01:49:51.180 回答