2015-04-18 72 views
0

我想要一些點遵循我的光標,但我無法刪除舊點,所以最終發生的是我在網格上獲得大量點。我已經嘗試過removeChild和其他一些方法,但都沒有工作。 Theres 2部分代碼,因爲我希望在它們之間交替,以便在運行時刪除/刪除其他代碼。所有這些都包含在一個每半秒運行一次的計時器中。從舞臺AS3中刪除所有影片剪輯的實例

if(radius<8 && counter%2!=0){ 
    xCoord = ((radius*Math.cos(theta))*25)+275; 
    yCoord = ((radius*Math.sin(theta))*(-1)*25)+225; 
    for(dots = 1; dots<=ship; dots++){ 
     var dotInstance2:dot = new dot(); 
     dotSprite2.addChild(dotInstance2); 
     mcArray2.push(dotInstance2); 
     dotInstance2.x=xCoord; 
     dotInstance2.y=yCoord; 
     xCoord = ((xCoord-275)/25); 
     yCoord = ((yCoord-225)/25)*(-1); 
     theta = Math.atan(yCoord/xCoord)-(15*(Math.PI/180)); 
     if(xCoord<0){ 
      theta = theta + Math.PI; 
     } 
     xCoord = ((radius*Math.cos(theta))*25)+275; 
     yCoord = ((radius*Math.sin(theta))*(-1)*25)+225; 
    } 
} 

的點都應該捕捉到極座標網格(我已經完成,那工作)我只需要能夠有比「船」更長時間後刪除舊點(數字)在舞臺上的點。

沒有測試方法去除這段代碼中的實例,因爲它們都沒有工作。 我已經試過被添加實例到一個數組,並從陣列中刪除所有的值,使用removeChild之和removeChildAt,還試圖刪除我的小精靈(dotSprite2在這個例子中)

回答

0
function removeDots(num:Number){ 
    for (var i:int=0; i<num; i++){   
    var dotInst:dot = mcArray2.shift(); 
    dotSprite2.removeChild(dotInst); 
    } 
} 

這應該工作。如果沒有,似乎有任何引用/聽衆點, 你沒有在你的代碼中顯示它們。

0

把所述點到一個數組是正確的方法,我會做這樣

var dots:Array = new Array() //your array that holds dots 
var dotsSprite:Sprite = new Sprite() //the sprite that holds the dots in its displaylist 
addChild(dotsSprite); 

//when you create your dots 
var dot = new dot(); 
dots.push(dot); 
dotsSprite.addChild(dot); 

//to destroy the first dot in your array (the oldest dot) 
dotsSprite.removeChild(dots.shift()); 

陣列的shift()方法移除所述陣列中的第一條目並且將這個其他一步向左(所以位置1變爲0,2變爲1等)。它也返回有問題的對象,所以我們可以將它作爲參數傳遞給dotsSprite.removeChild(),所以它將從其父項顯示列表中移除。

你的問題並不十分清楚,所以我假設你沒有試圖從你的dotsSprite2中刪除孩子,而是試圖從你的代碼運行removeChild((the dot))時刪除它。

從本質上講,你的計時器的方法應該是這樣的

function tick(e:TimerEvent):void{ 
    //this is the event listener for the timer you mentioned 
    //first we should check if there are old dots already on the stage 
    //we can set the amount of old dots that should stay before they're removed 
    if(dots.length>(amount of old dots you want to stay){ 
     //remove a dot in this timer tick 
     dotsSprite.removeChild(dots.shift()); 
    } 

    //then create a new dot, unless one at the current location already exists 
} 

這可能仍有待提高,例如,你可以定義爲點的生命週期,一個點可以在舞臺上的時間量。對於這一點,我建議是這樣的:

在你的類,創建和銷燬點

const dot_life_time:int = 3000; //3000ms = 3s 

在你點類

public var created:int = 0; 

當您創建點,給他們swf的當前運行時間(以毫秒爲單位)通過Flash的getTimer()方法

//the dot creation part comes here 
dot.created = getTimer(); 
//add it to the sprite and array 

現在,而不是檢查,如果數組是在一個特定的長度,你應該通過陣列中的每個點,檢查其時間超過

function tick(e:TimerEvent):void{ 
    for(var i:int=0;i<dots.length;i++){ 
     var elapsed:int = getTimer() - dots[i].created; 
     if(elapsed >= dot_life_time){ 
      //destroy the dot 
     } 
    } 

    //then, check if the current location already has a dot, if not, add one 
} 

這是所有未經測試,只是從我的頭頂,但我希望你能明白我所說的一般要點。