AS3删除精灵对象的多个子节点

时间:2013-07-20 01:12:04

标签: actionscript-3

我在应用中有一个按钮,用户可以单击该按钮添加多个输入字段。功能如下。

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);
}

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

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

由于

1 个答案:

答案 0 :(得分: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();
}