我在應用程序中有一個按鈕,用戶可以點擊添加多個輸入字段。該功能如下。AS3移除一個精靈對象的多個子對象
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。
我試着創建另一個精靈來保存fieldHolder的所有實例,但沒有奏效。我也試過removeChildren,但那也沒有效果。
感謝
當您調用'this.fiedHolder.removeChildren();'removeInviteFriendClick''時,會發生什麼情況,孩子們不會被刪除? –
它刪除所有輸入和刪除按鈕不僅是被點擊的'組' – puks1978
檢查我的答案。 –