2014-02-09 26 views
0

我「認爲」我所做的是正確的,但它不能。想象一下浮現在場景中間的物體,我們可以稱之爲gbl.obj。隨機地將其他元素添加到場景中,並且如果gbl.obj達到一個,則遊戲結束。此後,如果您願意,我想「重置」。重置包括刪除添加的元素。我不知道引用這些對象的最佳方法,我願意打賭這是我的問題。我將它們添加如下:ActionScript 3:添加到場景中的項目,一旦命中,不能被刪除/發現刪除

var bottom = randomRange(75,250); 
var pipeTop:Shape = new Shape; // initializing the variable named rectangle 
pipeTop.graphics.beginFill(0xFF0000); // choosing the colour for the fill, here it is red 
pipeTop.graphics.drawRect(0, 0, 75,bottom); // (x spacing, y spacing, width, height) 
pipeTop.graphics.endFill(); // not always needed but I like to put it in to end the fill 
pipeTop.addEventListener(Event.ENTER_FRAME, hitTarget); 
pipeTop.name = 'pipe'; 
addChild(pipeTop); 
var pipeBottom:Shape = new Shape; // initializing the variable named rectangle 
pipeBottom.graphics.beginFill(0x00FF00); // choosing the colour for the fill, here it is red 
pipeBottom.graphics.drawRect(0, bottom+125, 75,480-177-bottom); // (x spacing, y spacing, width, height) 
pipeBottom.graphics.endFill(); // not always needed but I like to put it in to end the fill 
pipeBottom.addEventListener(Event.ENTER_FRAME, hitTarget); 
pipeBottom.name = 'pipe'; 
addChild(pipeBottom); 
var topPipe = new Tween(pipeTop, 'x', None.easeIn,400,pipeTop.x-1000,200,false); 
var bottomPipe = new Tween(pipeBottom, 'x', None.easeIn,400,pipeBottom.x-1000,200,false); 

function hitTarget(e:Event):void{ 
    if (gbl.obj.hitTestObject(pipeTop) == true || gbl.obj.hitTestObject(pipeBottom) == true) { 
    topPipe.stop(); 
    bottomPipe.stop(); 
    pipeTop.removeEventListener(Event.ENTER_FRAME, hitTarget); 
    pipeBottom.removeEventListener(Event.ENTER_FRAME, hitTarget); 
    fail(); 
    } 
} 

基本上這些項目在屏幕上移動。當被擊中時,我阻止他們可以調用失敗函數,從本質上阻止所有動畫。然後按下復位按鈕時,出現這種情況:

Object(this).Overlay.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler); 
startGround(); 
gbl.obj.y = 200; 
gbl.timer = setInterval(doPipe,2300); 
fall(); 
gbl.gameOver = false; 
for(var i = 0; i < this.numChildren; i++){ 
    var item = this.getChildAt(i); 
    if(item.name == 'pipe'){ 
    this.removeChildAt(i); 
    } 
} 

現在每次發生的事情是所有這些管道的獲得除了一個我的目標沒有擊中刪除。當我試圖追蹤場景中所有兒童的物體時,它都不在那裏。當我刪除任何檢查,並刪除所有的孩子,它保持。元素去了哪裏?

回答

0

清理舞臺的最快方法是將臨時對象放在自己的容器中。雖然您的迭代子元素的方法應該可行,但使用容器作爲可移動對象會使清理成爲一站式命令。

var bottom = randomRange(75,250); 
var container:Sprite; 
var pipeBottom:Shape, pipeTop:Shape; 
var topPipe, bottomPipe; 

function setup():void { 
    container = new Sprite(); 
    addChild(container); 

    pipeTop = new Shape; // initializing the variable named rectangle 
    pipeTop.graphics.beginFill(0xFF0000); // choosing the colour for the fill, here it is red 
    pipeTop.graphics.drawRect(0, 0, 75,bottom); // (x spacing, y spacing, width, height) 
    pipeTop.graphics.endFill(); // not always needed but I like to put it in to end the fill 
    pipeTop.addEventListener(Event.ENTER_FRAME, hitTarget); 
    pipeTop.name = 'pipe'; 
    container.addChild(pipeTop); 

    pipeBottom = new Shape; // initializing the variable named rectangle 
    pipeBottom.graphics.beginFill(0x00FF00); // choosing the colour for the fill, here it is red 
    pipeBottom.graphics.drawRect(0, bottom+125, 75,480-177-bottom); // (x spacing, y spacing, width, height) 
    pipeBottom.graphics.endFill(); // not always needed but I like to put it in to end the fill 
    pipeBottom.addEventListener(Event.ENTER_FRAME, hitTarget); 
    pipeBottom.name = 'pipe'; 
    container.container.addChild(pipeBottom); 

    topPipe = new Tween(pipeTop, 'x', None.easeIn,400,pipeTop.x-1000,200,false); 
    bottomPipe = new Tween(pipeBottom, 'x', None.easeIn,400,pipeBottom.x-1000,200,false); 
} 

function hitTarget(e:Event):void{ 
    if (gbl.obj.hitTestObject(pipeTop) == true || gbl.obj.hitTestObject(pipeBottom) == true) { 
     topPipe.stop(); 
     bottomPipe.stop(); 
     pipeTop.removeEventListener(Event.ENTER_FRAME, hitTarget); 
     pipeBottom.removeEventListener(Event.ENTER_FRAME, hitTarget); 
     fail(); 
    } 
} 

function reset():void { 
    removeChild(container); 
    setup(); 
} 
+0

使用你的方法,我不能在舞臺上有一個以上的topPipe/bottomPipe正確嗎?我想我可以將容器對象傳遞給hitTarget函數,而不是查看全局變量。 – Leeish

+0

你可以擁有儘可能多的「管道」。任何實例化對象都有一個默認的唯一標識名(例如「instance355」),並且從實際的角度來看,子元素只是數字索引數組(DisplayListContainer)的屬性。你共享的例子只創建了2個管道。如果您需要更多,爲什麼不抽象該部分,並使用循環來生成X數量的管道。 – Atriace

相關問題