问题
For start I have to say that adding - removing child in AS3 is something new to me...
I have managed to add anything I want but the problem is that when a new result occur the previous has to disappear and the new will display in the same spot. Well the problem is here. They appear producting childs one over the other. Here is the code where i tried to removeChild using an if
//--------------------------μεταβλητές-----------------------------
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
}
It seems that "if" is not working... How can I check if there is something to remove or not...
I put the full code and the "if" is with ** at the start and at the end!
回答1:
var bathmos0:MovieClip = new oloioibathmoi[RankN[rankupday[0]]-1];
... this variable (bathmos0) is local to the scope of your function. It doesn't exist at the beginning of your function, when you try to remove it with:
if(bathmos0 && bathmos0.parent)
{
bathmos0.parent.removeChild(bathmos1);
bathmos0= null;
}
If you want to remove the clip, give it a name:
var bathmos0:MovieClip = new oloioibathmoi[RankN[rankupday[0]]-1];
bathmos0.name = "bathmos0";
... and then remove it by name:
var mc:MovieClip = getChildByName("bathmos0");
if(mc && mc.parent)
{
mc.parent.removeChild(mc);
mc= null;
}
Either that, or put your bathmosN
variables outside your function, so they are in scope when you try to remove them.
回答2:
I usually have a function like so:
private function remove(child:DisplayObject):void
{
if(child && child.parent)
{
child.parent.removeChild(child);
child = null;
}
}
As you can see it checks that the child exists (not null), then if it has a parent, and the parent contains the child on display list, if so it removes it and then nulls it, you may or may not want the child null, that's your choice.
回答3:
Is there any error? What do you mean 'not working'? And to remove child you have to check more than one condition. Try this:
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));
}
回答4:
It's a little clearer if you nest your conditions, checking if your target MovieClip is null before you check if its parent is null.
This code cannot logically throw a null exception error:
if(bathmos1 != null)
{
trace("bathmos1 is not null");
if(bathmos1.parent != null)
{
trace("bathmos1 parent is not null");
bathmos1.parent.removeChild(bathmos1);
}
bathmos1 = null;
}
So, to borrow from Neil, your reusable function would be:
private function removeIfExists($child:DisplayObject):void
{
if($child)
{
if($child.parent)
{
$child.parent.removeChild($child);
}
$child= null;
}
}
来源:https://stackoverflow.com/questions/12075417/as3-if-or-removechild-not-working