as3 removeChild - 我不能针对特定的孩子

时间:2012-04-19 12:25:08

标签: actionscript-3 flash

我仍然试图绕过AS3并且无法弄清楚如何在我的项目中定位一些特定的儿童并将其移除......我确信在第1页上有一个简单的解决方案“基本的AS3”书在某处,但我无法解决它!

我正在尝试删除addNewPoint函数中创建的红线(或者只是给它们一个0的alpha值)作为fillDriveway函数的一部分。

如果需要,我可以为您提供源文件的链接。

提前非常感谢!

这是我可怕的代码:

    import flash.display.*

pic.addEventListener(MouseEvent.CLICK,addNewPoint);

var n:Number = 0;

function addNewPoint(e:MouseEvent):void {
    n++;
    pointNo.text = String(n);
    var nextPoint:MovieClip = new newPoint();
    addChild(nextPoint);
    nextPoint.name = "mc"+pointNo.text;
    nextPoint.x = e.target.mouseX;
    nextPoint.y = e.target.mouseY;

    var joinPoints:MovieClip = new MovieClip();
    this.addChild(joinPoints);
    joinPoints.graphics.lineStyle(0.5,0xFF0000);
    joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
    for(var i:int=2; i<=n; ++i){
        joinPoints.graphics.lineTo(this.getChildByName("mc"+i).x, this.getChildByName("mc"+i).y);
    }
}

pic.addEventListener(MouseEvent.CLICK, addNewPoint);

function fillDriveway(eventObject:MouseEvent) {
    var joinPoints:MovieClip = new MovieClip();
    this.addChild(joinPoints);
    joinPoints.graphics.beginFill(0xFFFFFF, 0.7);
    joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
    for(var m:int=2; m<=n; ++m){
        joinPoints.graphics.lineTo(this.getChildByName("mc"+m).x, this.getChildByName("mc"+m).y);
    }
    joinPoints.name = "driveshape";
    filledDrive.text = "filled";
}

function undoit(eventObject:MouseEvent) {
    if(n > 0) {
        if(filledDrive.text.indexOf("filled") != -1) {
            this.removeChild(this.getChildAt(this.numChildren -1));
            filledDrive.text = "";
            }else{
            this.removeChild(this.getChildAt(this.numChildren -1));
            this.removeChild(this.getChildAt(this.numChildren -1));
            n--;
            pointNo.text = String(n);
        }
    }
}

undo.addEventListener(MouseEvent.CLICK, undoit);

function maskDrive(eventObject:MouseEvent) {
    if(filledDrive.text.indexOf("filled") != -1) {
        var finishA:MovieClip = new finishMC();
        this.addChild(finishA);
        finishA.x = 310;
        finishA.y = 100;
        finishA.mask = getChildByName("driveshape");
        finishA.gotoAndPlay(2);
    }
}

//BTN Actions
function btn1over(myEvent:MouseEvent) {
    btn1.gotoAndPlay(2);
}
function btn1out(myEvent:MouseEvent) {
    btn1.gotoAndPlay(11);
}
function btn2over(myEvent:MouseEvent) {
    btn2.gotoAndPlay(2);
}
function btn2out(myEvent:MouseEvent) {
    btn2.gotoAndPlay(11);
}
function btn3over(myEvent:MouseEvent) {
    btn3.gotoAndPlay(2);
}
function btn3out(myEvent:MouseEvent) {
    btn3.gotoAndPlay(11);
}

//BTN Calls
btn1.addEventListener(MouseEvent.CLICK, fillDriveway);
btn1.addEventListener(MouseEvent.ROLL_OVER, btn1over);
btn1.addEventListener(MouseEvent.ROLL_OUT, btn1out);
btn1.buttonMode = true;
btn1.useHandCursor = true;
btn2.addEventListener(MouseEvent.CLICK, maskDrive);
btn2.addEventListener(MouseEvent.ROLL_OVER, btn2over);
btn2.addEventListener(MouseEvent.ROLL_OUT, btn2out);
btn2.buttonMode = true;
btn2.useHandCursor = true;
btn3.buttonMode = true;
btn3.useHandCursor = true;
btn3.addEventListener(MouseEvent.ROLL_OVER, btn3over);
btn3.addEventListener(MouseEvent.ROLL_OUT, btn3out);

1 个答案:

答案 0 :(得分:0)

我认为您的问题是,每次创建新的joinPoints时,您都会覆盖指向前一个joinPoints实例的指针,因此您无法访问它。 (这会造成内存泄漏)。

每次创建joinPoints对象时,请将其添加到数组中:

joinPointsArray.push(joinPoints);

您的fillDriveway功能可以访问您创建的所有joinPoints:

for (var i:int = 0; i < joinPointsArray.length, i++) {
joinPointsArray[i].alpha = 0;
}