2012-04-19 44 views
0

我仍然在試圖讓我的頭周圍AS3和無法弄清楚如何定位一些特定的孩子在我的項目並刪除它們。我敢肯定有一個簡單的在「Basic AS3」書籍的第1頁上找到解決方法,但是我無法解決這個問題!AS3 removeChild之 - 我不能針對特定的兒童

我試圖消除在addNewPoint函數創建的紅線(或只是給他們的0阿爾法)作爲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); 
+0

你想要做什麼會發生什麼?你的代碼很長,它需要一些能量來理解它:) – Kodiak 2012-04-19 12:27:55

+0

是的,它很抱歉!該addNewPoint函數將在那裏用戶點擊一個MC,那麼以後每單擊它在這一點上又增加了MC和從先前的點用一條紅線新點連接。然後,fillDriveway函數填充此繪製的形狀,以便我可以使用它來掩蓋圖像。但是,當fillDriveway叫我想它首先刪除所有畫的紅線(不是儘管這些點MCS)... – bigtoothmedia 2012-04-19 12:40:14

回答

0

我覺得你的問題是,每次創建要覆蓋指向前一joinPoints實例的新joinPoints,因此,你不能訪問它。 (這會造成內存泄漏)。

每次創建一個joinPoints對象,將其添加到陣列時間:

joinPointsArray.push(joinPoints); 

你fillDriveway功能則可以訪問您所創建的所有joinPoints:

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

尼斯一個感謝crooksy88的 - 我有與本項目的另外一個問題,所以我會在彈出它現在......只有你一個人! – bigtoothmedia 2012-04-19 13:18:08