ステージから子供を削除することはできません-if(stage.contains(child1)){removechild(child1);}

StackOverflow https://stackoverflow.com/questions/9316758

  •  26-10-2019
  •  | 
  •  

質問

愚かに聞こえるかもしれませんが、どうすればステージから明確な子供を取り除くことができますか?例えば

function giveMeResult(e:MouseEvent):void
{

if(stage.contains(result))
    {removeChild(result);}

    addChild(result); // this part works fine, but it adds one over another
}

アップデート:*結果はテキストフィールドであり、result.txt = ""

trace (result.parent); /// gives  [object Stage]
trace (result.stage); /// gives[object Stage]

trace (result.parent != null && result.parent == result.stage); // gives true

いつ

result.parent.removeChild(result);

TypeError:エラー#1009:nullオブジェクト参照のプロパティまたは方法にアクセスできません。

内部に書かれたとき:

if (result.parent !=null && result.parent == result.stage)
{
result.parent.removeChild(result);
}

何も起こりません。前の子供の上部に新しい子供が追加されます。

ありがとうございます!!!結果は単純です:)私がしなければならなかったのは、ステージからそれを削除せずにresult.txtを変更するだけです:)

役に立ちましたか?

解決 5

私がしなければならなかったのは、ステージから削除せずにresult.txtを変更するだけです

他のヒント

入力する必要があります stage.removeChild(result); そしてその後 stage.addChild(result);

編集:

あなたのものに似た関数を見る:

private function func(e:Event) : void {
    if(stage.contains(result)) {
        stage.removeChild(result);
    }

    stage.addChild(result);
}

これがテキストフィールドの新しいインスタンスを追加する唯一の方法 result ステージには、古いものを削除せずに、結果が変わった場合になります。この実行の流れを参照してください:

var result : TextField = new TextField();

// An event occurs and func get's called.
// now result will be added to stage.

result = new TextField();

// An event occurs and func get's called again
// This time the stage.contains(..) will return false, since the current result
// is not actually on stage. This will add a second TextField to the stage.

私があなたが望むものを明確に理解しているなら、このコードはあなたを助けることができます:

if (result.parent != null && result.parent == result.stage)
{
    // stage itself contains result
    result.parent.removeChild(result);
}

のドキュメントから displayObjectContainer.contains(): :「孫、great孫など、それぞれが真実です。」

したがって、監督の子供だけでなく、ディスプレイリストのどこでも意味があります。あなたが望むのは、マンクが指摘した親のチェック、または単にどこからでも結果を削除することです。

if (result.parent) {result.parent.removeChild(result); }

奇妙なことに、AddChild(結果)は前の親から自動的にそれを削除します-DisplayObjectは一度にディスプレイリストの1つの場所にしか存在できないため、複数の結果が表示される理由はわかりません...

あなたが渡した「結果」がすでにステージ上にある結果ではない可能性はありますか?

やってみました?

MovieClip(root).removeChild(result)

編集

function giveMeResult(e:MouseEvent):void{
  if(result.parent != null && result.parent == result.stage){
    stage.removeChild(result);
  }

   stage.addChild(result); 
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top