0

首先,我不得不说在 AS3 中添加 - 删除子项对我来说是新事物......

我设法添加了我想要的任何东西,但问题是当出现新结果时,前一个结果必须消失,而新结果将显示在同一个位置。那么问题就在这里。他们似乎一个接一个地生产孩子。这是我尝试使用 if 删除Child 的代码

//--------------------------μεταβλητές-----------------------------
var iStr:String;
var iffpD:String;
var iRankP:String;
var iq0:Number;
var iq1:String;
var iq2:String;
var iq3:String;
var iq4:String;
var iq5:String;
var iq6:String;
var iq7:String;
var iFree:String;
var iclimc:String;
var ishootr:String;
var ispecialf:String;
var Ranked:Number;
var i:Number;
var day:Number;
var Powered:Number;
var PPD:Number;
var RankN:Array = new Array(0);
var rankupday:Array = new Array(0);
var rankpointup:Array = new Array(0);
var apotelesma:Number;
var oloioibathmoi:Array = new Array(P0,P1,P2,P3,CO0,CO1,CO2,CO3,S0,S1,S2,S3,L0,L1,L2,L3,CPT0,CPT1,CPT2,CPT3,M0,M1,M2,M3,COM0,COM1,COM2,COM3,LC0,LC1,LC2,LC3,C0,C1,C2,C3,G0,G1,G2,G3,FM0,FM1,FM2,FM3,SM0,SM1,SM2,SM3,NF0,NF1,NF2,NF3,WCF0,WCF1,WCF2,WCF3,LF0,LF1,LF2,LF3,Gow0,Gow1,Gow2,Gow3)


//----------------------------restricts--------------------------------
Str.restrict = "0-9\\.";
ffpD.restrict = "0-9";
RankP.restrict = "0-9";
q1.restrict = "0-9";
q2.restrict = "0-9";
q3.restrict = "0-9";
q4.restrict = "0-9";
q5.restrict = "0-9";
q6.restrict = "0-9";
q7.restrict = "0-9";
Free.restrict = "1-3";
climc.restrict = "0-3";
shootr.restrict = "0-3";
specialf.restrict = "0-3";

//--------------------------borders--------------------------------------
Str.border = true;
ffpD.border = true;
RankP.border = true;
q1.border = true;
q2.border = true;
q3.border = true;
q4.border = true;
q5.border = true;
q6.border = true;
q7.border = true;
Free.border = true;
climc.border = true;
shootr.border = true;
specialf.border = true;

//--------------------i = noti.text-------------------------------------

iStr= Str.text;
iffpD= ffpD.text;
iRankP= RankP.text;
iq1= q1.text;
iq2= q2.text;
iq3= q3.text;
iq4= q4.text;
iq5= q5.text;
iq6= q6.text;
iq7= q7.text;
iFree= Free.text;
iclimc= climc.text;
ishootr= shootr.text;
ispecialf= specialf.text;
day = 0
i = 0
apotelesma = 0
//---------------------button------------------------------------------

btn.addEventListener(MouseEvent.CLICK, Calco);

function Calco(event:MouseEvent):void
{

        **if(bathmos1 && bathmos1.parent)
        {
            bathmos1.parent.removeChild(bathmos1);
            bathmos1 = null;
        }


        if(bathmos0 && bathmos0.parent)
        {
            bathmos0.parent.removeChild(bathmos0);
            bathmos0 = null;
        }


        if(bathmos2 && bathmos2.parent)
        {
            bathmos2.parent.removeChild(bathmos2);
            bathmos2 = null;
        }**



    iStr= Str.text;
    iffpD= ffpD.text;
    iRankP= RankP.text;

    iq1= q1.text;
    iq2= q2.text;
    iq3= q3.text;
    iq4= q4.text;
    iq5= q5.text;
    iq6= q6.text;
    iq7= q7.text;
    iFree= Free.text;
    iclimc= climc.text;
    ishootr= shootr.text;
    ispecialf= specialf.text;
    Ranked = Number(RankP); 

    Powered = Number(iStr);
    PPD = (Number(iFree) * 5) + (Number(iclimc) * 5/2) + (Number(ishootr) * 5) + (Number(ispecialf) * 10) + (9 / 5)
    //------------------------------------------rank finder RankN--------------------------------
    if (Number(iRankP) < 15) {
        RankN[0] = "1"
    } else if (Number(iRankP) < 45) {
        RankN[0] = "2"
    } else if (Number(iRankP) < 80 ) {
        RankN[0] = "3"
    } else if (Number(iRankP) < 120 ) {
        RankN[0] = "4"
    } else if (Number(iRankP) < 170 ) {
        RankN[0] = "5"
    } else if (Number(iRankP) < 250 ) {
        RankN[0] = "6" 
    } else if (Number(iRankP) < 350 ) {
        RankN[0] = "7"
    } else if (Number(iRankP) < 450 ) {
        RankN[0] = "8"
    } else if (Number(iRankP) < 600 ) {
        RankN[0] = "9"
    } else if (Number(iRankP) < 800 ) {
        RankN[0] = "10"
    } else if (Number(iRankP) < 1000 ) {
        RankN[0] = "11" 
    } else if (Number(iRankP) < 1400 ) {
        RankN[0] = "12" 
    } else if (Number(iRankP) < 1850 ) {
        RankN[0] = "13"
    } else if (Number(iRankP) < 2350 ) {
        RankN[0] = "14"
    } else if (Number(iRankP) < 3000 ) {
        RankN[0] = "15"
    } else if (Number(iRankP) < 3750 ) {
        RankN[0] = "16"
    } else if (Number(iRankP) < 5000 ) {
        RankN[0] = "17"
    } else if (Number(iRankP) < 6500 ) {
        RankN[0] = "18"
    } else if (Number(iRankP) < 9000 ) {
        RankN[0] = "19"
    } else if (Number(iRankP) < 12000 ) {
        RankN[0] = "20"
    } else if (Number(iRankP) < 15500 ) {
        RankN[0] = "21"
    } else if (Number(iRankP) < 20000 ) {
        RankN[0] = "22"
    } else if (Number(iRankP) < 25000 ) {
        RankN[0] = "23"
    } else if (Number(iRankP) < 31000 ) {
        RankN[0] = "24"
    } else if (Number(iRankP) < 40000 ) {
        RankN[0] = "25"
    } else if (Number(iRankP) < 52000 ) {
        RankN[0] = "26"
    } else if (Number(iRankP) < 67000 ) {
        RankN[0] = "27"
    } else if (Number(iRankP) < 85000 ) {
        RankN[0] = "28"
    } else if (Number(iRankP) < 110000 ) {
        RankN[0] = "29"
    } else if (Number(iRankP) < 140000 ) {
        RankN[0] = "30"
    } else if (Number(iRankP) < 180000 ) {
        RankN[0] = "31"
    } else if (Number(iRankP) < 225000 ) {
        RankN[0] = "32"
    } else if (Number(iRankP) < 285000 ) {
        RankN[0] = "33"
    } else if (Number(iRankP) < 355000 ) {
        RankN[0] = "34"
    } else if (Number(iRankP) < 435000 ) {
        RankN[0] = "35"
    } else if (Number(iRankP) < 540000 ) {
        RankN[0] = "36"
    } else if (Number(iRankP) < 660000 ) {
        RankN[0] = "37"
    } else if (Number(iRankP) < 800000 ) {
        RankN[0] = "38"
    } else if (Number(iRankP) < 950000 ) {
        RankN[0] = "39"
    } else if (Number(iRankP) < 1140000 ) {
        RankN[0] = "40"
    } else if (Number(iRankP) < 1350000 ) {
        RankN[0] = "41"
    } else if (Number(iRankP) < 1600000 ) {
        RankN[0] = "42"
    } else if (Number(iRankP) < 1875000 ) {
        RankN[0] = "43"
    } else if (Number(iRankP) < 2185000 ) {
        RankN[0] = "44"
    } else if (Number(iRankP) < 2550000 ) {
        RankN[0] = "45"
    } else if (Number(iRankP) < 3000000 ) {
        RankN[0] = "46"
    } else if (Number(iRankP) < 3500000 ) {
        RankN[0] = "47"
    } else if (Number(iRankP) < 4150000 ) {
        RankN[0] = "48"
    } else if (Number(iRankP) < 4900000 ) {
        RankN[0] = "49"
    } else if (Number(iRankP) < 5800000 ) {
        RankN[0] = "50"
    } else if (Number(iRankP) < 7000000 ) {
        RankN[0] = "51"
    } else if (Number(iRankP) < 9000000 ) {
        RankN[0] = "52"
    } else if (Number(iRankP) < 11500000 ) {
        RankN[0] = "53"
    } else if (Number(iRankP) < 14500000 ) {
        RankN[0] = "54"
    } else if (Number(iRankP) < 18000000 ) {
        RankN[0] = "55"
    } else if (Number(iRankP) < 22000000 ) {
        RankN[0] = "56"
    } else if (Number(iRankP) < 26500000 ) {
        RankN[0] = "57"
    } else if (Number(iRankP) < 31500000 ) {
        RankN[0] = "58"
    } else if (Number(iRankP) < 37000000 ) {
        RankN[0] = "59"
    } else if (Number(iRankP) < 43000000 ) {
        RankN[0] = "60"
    } else if (Number(iRankP) < 50000000 ) {
        RankN[0] = "61"
    } else if (Number(iRankP) < 100000000 ) {
        RankN[0] = "62"
    } else if (Number(iRankP) < 200000000 ) {
        RankN[0] = "63"
    } else if (Number(iRankP) < 500000000 ) {
        RankN[0] = "64"
    } else if (Number(iRankP) > 500000000 ) {
        RankN[0] = "65"
    }



    ertext.text = ""
    iq0 = Number(iffpD) - Number(iq1)-Number(iq2) -Number(iq3)-Number(iq4)-Number(iq5)-Number(iq6)-Number(iq7)
    if (Number(iStr) == 0 || Number(iffpD) >= 250 || Number(iffpD) == 0 || iq0 < 0 ) {
        ertext.text = "Fill Strenght, FFs per day, rank points!"
    } else {

        //while (RankN[day] != "65"){
        while (Number(iRankP) < 500000000){
            if (Number(iRankP) < 15) {
        RankN[day] = "1"
    } else if (Number(iRankP) < 45) {
        RankN[day] = "2"
    } else if (Number(iRankP) < 80 ) {
        RankN[day] = "3"
    } else if (Number(iRankP) < 120 ) {
        RankN[day] = "4"
    } else if (Number(iRankP) < 170 ) {
        RankN[day] = "5"
    } else if (Number(iRankP) < 250 ) {
        RankN[day] = "6" 
    } else if (Number(iRankP) < 350 ) {
        RankN[day] = "7"
    } else if (Number(iRankP) < 450 ) {
        RankN[day] = "8"
    } else if (Number(iRankP) < 600 ) {
        RankN[day] = "9"
    } else if (Number(iRankP) < 800 ) {
        RankN[day] = "10"
    } else if (Number(iRankP) < 1000 ) {
        RankN[day] = "11" 
    } else if (Number(iRankP) < 1400 ) {
        RankN[day] = "12" 
    } else if (Number(iRankP) < 1850 ) {
        RankN[day] = "13"
    } else if (Number(iRankP) < 2350 ) {
        RankN[day] = "14"
    } else if (Number(iRankP) < 3000 ) {
        RankN[day] = "15"
    } else if (Number(iRankP) < 3750 ) {
        RankN[day] = "16"
    } else if (Number(iRankP) < 5000 ) {
        RankN[day] = "17"
    } else if (Number(iRankP) < 6500 ) {
        RankN[day] = "18"
    } else if (Number(iRankP) < 9000 ) {
        RankN[day] = "19"
    } else if (Number(iRankP) < 12000 ) {
        RankN[day] = "20"
    } else if (Number(iRankP) < 15500 ) {
        RankN[day] = "21"
    } else if (Number(iRankP) < 20000 ) {
        RankN[day] = "22"
    } else if (Number(iRankP) < 25000 ) {
        RankN[day] = "23"
    } else if (Number(iRankP) < 31000 ) {
        RankN[day] = "24"
    } else if (Number(iRankP) < 40000 ) {
        RankN[day] = "25"
    } else if (Number(iRankP) < 52000 ) {
        RankN[day] = "26"
    } else if (Number(iRankP) < 67000 ) {
        RankN[day] = "27"
    } else if (Number(iRankP) < 85000 ) {
        RankN[day] = "28"
    } else if (Number(iRankP) < 110000 ) {
        RankN[day] = "29"
    } else if (Number(iRankP) < 140000 ) {
        RankN[day] = "30"
    } else if (Number(iRankP) < 180000 ) {
        RankN[day] = "31"
    } else if (Number(iRankP) < 225000 ) {
        RankN[day] = "32"
    } else if (Number(iRankP) < 285000 ) {
        RankN[day] = "33"
    } else if (Number(iRankP) < 355000 ) {
        RankN[day] = "34"
    } else if (Number(iRankP) < 435000 ) {
        RankN[day] = "35"
    } else if (Number(iRankP) < 540000 ) {
        RankN[day] = "36"
    } else if (Number(iRankP) < 660000 ) {
        RankN[day] = "37"
    } else if (Number(iRankP) < 800000 ) {
        RankN[day] = "38"
    } else if (Number(iRankP) < 950000 ) {
        RankN[day] = "39"
    } else if (Number(iRankP) < 1140000 ) {
        RankN[day] = "40"
    } else if (Number(iRankP) < 1350000 ) {
        RankN[day] = "41"
    } else if (Number(iRankP) < 1600000 ) {
        RankN[day] = "42"
    } else if (Number(iRankP) < 1875000 ) {
        RankN[day] = "43"
    } else if (Number(iRankP) < 2185000 ) {
        RankN[day] = "44"
    } else if (Number(iRankP) < 2550000 ) {
        RankN[day] = "45"
    } else if (Number(iRankP) < 3000000 ) {
        RankN[day] = "46"
    } else if (Number(iRankP) < 3500000 ) {
        RankN[day] = "47"
    } else if (Number(iRankP) < 4150000 ) {
        RankN[day] = "48"
    } else if (Number(iRankP) < 4900000 ) {
        RankN[day] = "49"
    } else if (Number(iRankP) < 5800000 ) {
        RankN[day] = "50"
    } else if (Number(iRankP) < 7000000 ) {
        RankN[day] = "51"
    } else if (Number(iRankP) < 9000000 ) {
        RankN[day] = "52"
    } else if (Number(iRankP) < 11500000 ) {
        RankN[day] = "53"
    } else if (Number(iRankP) < 14500000 ) {
        RankN[day] = "54"
    } else if (Number(iRankP) < 18000000 ) {
        RankN[day] = "55"
    } else if (Number(iRankP) < 22000000 ) {
        RankN[day] = "56"
    } else if (Number(iRankP) < 26500000 ) {
        RankN[day] = "57"
    } else if (Number(iRankP) < 31500000 ) {
        RankN[day] = "58"
    } else if (Number(iRankP) < 37000000 ) {
        RankN[day] = "59"
    } else if (Number(iRankP) < 43000000 ) {
        RankN[day] = "60"
    } else if (Number(iRankP) < 50000000 ) {
        RankN[day] = "61"
    } else if (Number(iRankP) < 100000000 ) {
        RankN[day] = "62"
    } else if (Number(iRankP) < 200000000 ) {
        RankN[day] = "63"
    } else if (Number(iRankP) < 500000000 ) {
        RankN[day] = "64"
    } else if (Number(iRankP) > 500000000 ) {
        RankN[day] = "65"
    }
            Powered = Powered + PPD
            apotelesma = ((((Number(RankN[day])-1)/20+(45/150))*((Powered/10)+40))* (1 + 0/100)* iq0) +
                         ((((Number(RankN[day])-1)/20+(45/150))*((Powered/10)+40))* (1 + 20/100)* Number(iq1)) +
                         ((((Number(RankN[day])-1)/20+(45/150))*((Powered/10)+40))* (1 + 40/100)* Number(iq2)) +
                         ((((Number(RankN[day])-1)/20+(45/150))*((Powered/10)+40))* (1 + 60/100)* Number(iq3)) +
                         ((((Number(RankN[day])-1)/20+(45/150))*((Powered/10)+40))* (1 + 80/100)* Number(iq4)) +
                         ((((Number(RankN[day])-1)/20+(45/150))*((Powered/10)+40))* (1 + 100/100)* Number(iq5)) +
                         ((((Number(RankN[day])-1)/20+(45/150))*((Powered/10)+40))* (1 + 120/100)* Number(iq6)) +
                         ((((Number(RankN[day])-1)/20+(45/150))*((Powered/10)+40))* (1 + 200/100)* Number(iq7));

            apotelesma = apotelesma / 10;
            iRankP = String(Number(iRankP) + apotelesma);


            if (Number(RankN[day]) > Number(RankN[day - 1])) {
                rankupday[i] = String(day);
                rankpointup[i] = iRankP
                i ++
            }
            if (Number(RankN[day]) == 62 && Number(RankN[day-1]) == 61) {
                Gow.text = String(day);
            }
            day = day + 1
        }
    }
    rank1.text = rankupday[0];
    rank2.text = rankupday[1];
    rank3.text = rankupday[2];
    Goww.text = String(day);





    var bathmos0:MovieClip = new oloioibathmoi[RankN[rankupday[0]]-1];
    addChild(bathmos0);
    bathmos0.x = 60;
    bathmos0.y = 535;
    bathmos0.alpha=.5;
    var bathmos1:MovieClip = new oloioibathmoi[RankN[rankupday[1]]-1];
    addChild(bathmos1);
    bathmos1.x = 156;
    bathmos1.y = 535;
    bathmos1.alpha=.5;
    var bathmos2:MovieClip = new oloioibathmoi[RankN[rankupday[2]]-1];
    addChild(bathmos2);
    bathmos2.x = 256;
    bathmos2.y = 535;
    bathmos2.alpha=.5;


    day=0
}

似乎“如果”不起作用......我如何检查是否有要删除的东西......
我放了完整的代码,“如果”在开头和结尾都带有**!

4

4 回答 4

2

我通常有这样的功能:

private function remove(child:DisplayObject):void
{
    if(child && child.parent)
    {
         child.parent.removeChild(child);
         child = null;
    }
}

如您所见,它检查子项是否存在(不为空),然后如果它有一个父项,并且父项包含显示列表中的子项,如果是这样,它将删除它然后将其清空,您可能想要也可能不想要孩子null,那是你的选择。

于 2012-08-22T15:29:22.440 回答
1
var bathmos0:MovieClip = new oloioibathmoi[RankN[rankupday[0]]-1];

...这个变量 (bathmos0) 在你的函数范围内是本地的。当您尝试使用以下方法将其删除时,它在您的功能开始时不存在:

if(bathmos0 && bathmos0.parent)
{
bathmos0.parent.removeChild(bathmos1);
bathmos0= null;
}

如果要删除剪辑,请为其命名:

var bathmos0:MovieClip = new oloioibathmoi[RankN[rankupday[0]]-1];
bathmos0.name = "bathmos0";

...然后按名称将其删除:

var mc:MovieClip = getChildByName("bathmos0");
if(mc && mc.parent)
{
    mc.parent.removeChild(mc);
    mc= null;
}

要么这样,要么把你的bathmosN变量放在你的函数之外,这样当你尝试删除它们时它们就在范围内。

于 2012-08-23T17:42:24.613 回答
0

如果嵌套条件会更清楚一些,在检查其父级是否为 null之前检查目标 MovieClip 是否为 null。

此代码在逻辑上无法抛出空异常错误:

if(bathmos1 != null)
{
    trace("bathmos1 is not null");
    if(bathmos1.parent != null)
    {
        trace("bathmos1 parent is not null");
        bathmos1.parent.removeChild(bathmos1);
    }
    bathmos1 = null;
}

因此,借用 Neil,您的可重用函数将是:

private function removeIfExists($child:DisplayObject):void
{
    if($child)
    {
        if($child.parent)
        {
            $child.parent.removeChild($child);
        }
        $child= null;
    }
}
于 2012-08-23T08:28:20.733 回答
0

有什么错误吗?你是什​​么意思'不工作'?要删除孩子,您必须检查多个条件。尝试这个:

if(bathmos1 != null && bathmos1.parent != null && stage.contains(bathmos1)){
  stage.removeChild(bathmos1);
}else
    trace("bathmos1 is not null: " + bathmos1 != null);
    trace("bathmos1's parent is not null: " + bathmos1.parent != null);
    trace("stage contains bathmos1: " + stage.contains(bathmos1));
}
于 2012-08-22T14:40:33.460 回答