删除由 class as3 创建的 Child

Remove Child created by class as3

我已经为一个 class 项目学习 AS3 几个月了,我所知道的大部分是通过查看其他问题是如何解决的。

进入正题。

我有一个名为 Generate 的 class,所以我将其导入到我的主时间轴中,因为 class 它会创建一个 child 并添加它。

现在我不知道如何删除它,它总是说它是空的: "TypeError: Error #2007: Parameter child must be non-null."

package  {

import MonsterOne;
import MonsterTwo;
import MonsterThree;
import MonsterFour;
import flash.display.*;

public class Generate extends MovieClip{

    public static var monsterID = String(monsterID);
    monsterID = Math.ceil(Math.random() * 4).toString();

    public function Generate(parent:Object){
        if (monsterID == 1){
            var monsOne:MonsterOne = new MonsterOne();
            monsOne.name = "monsterOne";
            parent.addChild(monsOne);
            monsOne.x = 100;
            monsOne.y = 200;
            trace("spawn1");
        }

这是它的生成方式。 monsTwo、3 和 4 的代码多了一点。相同的代码不同的名称。这就是文件中的所有内容。

我尝试了各种方法来删除代码,但总是出现错误并且没有实际删除 child。

stage.removeChild(monsOne);
monsOne.parent.removeChild(monsOne);
removeChild(monsOne);

谁知道还有多少人。

我是漏掉了什么还是完全错了。

谢谢

--编辑--

if(o.monHP <= 0) {
    turnTimer.stop();
    turnTimer.removeEventListener(TimerEvent.TIMER, CounterA);
    var monsOne:MovieClip = getChildByName('monsterOne') as MovieClip;
    var monsTwo:MovieClip = getChildByName('monsterTwo') as MovieClip;
    var monsThree:MovieClip = getChildByName('monsterThree') as MovieClip;
    var monsFour:MovieClip = getChildByName('monsterFour') as MovieClip;
    parent.removeChild(monsOne);
    parent.removeChild(monsTwo);
    parent.removeChild(monsThree);
    parent.removeChild(monsFour);

    gotoAndStop('win');
}

我如何尝试删除 child。

package  {

import MonsterOne;
import MonsterTwo;
import MonsterThree;
import MonsterFour;
import flash.display.*;

public class Generate extends MovieClip{

    public static var monsterID = String(monsterID);
    monsterID = Math.ceil(Math.random() * 4).toString();

    public function Generate(parent:Object){
        if (monsterID == 1){
            var monsOne:MonsterOne = new MonsterOne();
            monsOne.name = "monsterOne";
            parent.addChild(monsOne);
            monsOne.x = 100;
            monsOne.y = 200;
            trace("spawn1");
        }

        if (monsterID == 2){
            var monsTwo:MonsterTwo = new MonsterTwo();
            monsTwo.name = "monsterTwo";
            parent.addChild(monsTwo);
            monsTwo.x = 100;
            monsTwo.y = 200;
            trace("spawn2");
        }

        if (monsterID == 3){
            var monsThree:MonsterThree = new MonsterThree();
            monsThree.name = "monsterThree";
            parent.addChild(monsThree);
            monsThree.x = 100;
            monsThree.y = 200;
            trace("spawn3");
        }

        if (monsterID == 4){
            var monsFour:MonsterFour = new MonsterFour();
            monsFour.name = "monsterFour";
            parent.addChild(monsFour);
            monsFour.x = 100;
            monsFour.y = 200;
            trace("spawn4");
        }
    }
}

} 完整生成文件

Generateclass只生成一只怪物。在 gameOver 函数中,你试图移除所有 4 个怪物,但你得到一个错误,提示怪物不存在。您应该添加条件:

if (monsOne) removeChild(monsOne);
if (monsTwo) removeChild(monsTwo);
if (monsThree) removeChild(monsThree);
if (monsFour) removeChild(monsFour);

您的代码中有太多错误,很难知道从哪里开始。您对显示列表和最重要的范围缺乏非常基本的了解。

显示列表:

当你做这样的事情时:

var monsOne:MovieClip = getChildByName('monsterOne') as MovieClip;
parent.removeChild(monsOne);

可以翻译成"no idea what I'm doing here"。为什么?因为代码意味着它期望名为 "monsterOne" 的 DisplayObject 存在于 "this" 的显示列表(范围内的显示列表)中,然后尝试从 "parent" 中删除 object . None 这是有道理的,如果 object 存在于显示列表 A 中,则将其从显示列表 A 中删除,而不是从显示列表 B 中删除。

编程:

您正在使用反映现有属性的变量和参数,例如:"parent"。您确定以您的编程水平可以承受这种危险的编程行为吗?答案是否定的,不要镜像属性名称。

范围:

你到处都在丢失作用域,却一直在编写代码,就像作用域甚至不存在一样。别搞了。一个范围内名为 monsOne 的变量在任何其他范围内都不存在,期间。

更多怪事:

public static var monsterID = String(monsterID);
monsterID = Math.ceil(Math.random() * 4).toString();

你显然不知道这是做什么的,否则你不会那样做。

你的代码是做什么的?:

它创建了一个而且只有一个电影剪辑类型,每次创建它时,它都会给它一个完全相同的名称并将它添加到一个我们一无所知的幻影"parent"。然后,当您尝试删除它时,您甚至不知道或不记得这个幻影 parent 是什么,您也不能真正使用 getChildByName() 因为您需要知道 parent 才能执行甚至更糟的是,到那时可能会有很多 MovieClip 并且都具有相同的名称。

如何解决?

此部分稍后会在您询问有关如何解决问题的具体问题时填写。