Flash CS3删除舞台上的对象

时间:2014-11-27 00:59:00

标签: arrays flash object flash-cs3

好的,所以我在主时间轴内有这个迷你游戏。迷你游戏使用addChild(new a0),new a1,new a2等在数组内动态创建一堆对象...无论如何,在游戏结束时,有一个选项可以重启(重置分数和去除)回到起始帧)或完成(返回几帧到"主屏幕"它在不同的层上并返回几帧。如果我选​​择任一选项,任何未删除的对象即使重新启动或回到主框架,玩游戏(获得匹配)也会留在舞台上。我已经尝试了各种方法调用removeChild,将数组设置为空,什么不是,我不能似乎弄清楚如何删除它们。使用我将在此处显示的代码,我收到此错误:

ArgumentError:错误#2025:提供的DisplayObject必须是调用者的子级。     在flash.display :: DisplayObjectContainer / removeChild()     在mousiesDay_fla :: MainTimeline / clearGame()[mousiesDay_fla.MainTimeline :: frame258:11]     在mousiesDay_fla :: MainTimeline / tryAgain()[mousiesDay_fla.MainTimeline :: frame258:29]

这是代码

    stop();
scoreWindow.visible = false;
scoreWindowText.visible = false;
finBtn.visible = false;
tryBtn.visible = false;
finBtn.removeEventListener(MouseEvent.CLICK, finished);
tryBtn.removeEventListener(MouseEvent.CLICK, tryAgain);

function clearGame() {
    for( var i:int = 0; i < numClips; i++ ) {
        removeChild( myClip[i] );

    }
    myClip.length = 0;
    scoreWindow.visible = false;
    scoreWindowText.visible = false;
    finBtn.visible = false;
    tryBtn.visible = false;
    finBtn.removeEventListener(MouseEvent.CLICK, finished);
    tryBtn.removeEventListener(MouseEvent.CLICK, tryAgain);
}

function finished(evt:MouseEvent) {
    clearGame();
    gotoAndPlay(256);
}

function tryAgain(evt:MouseEvent) {
    clearGame();
    gotoAndPlay(257);
}

backBtn.addEventListener(MouseEvent.CLICK, goBack);

function goBack(evt:MouseEvent) {
    gotoAndPlay(256);
}

import flash.utils.*;

var myTimer:Timer = new Timer(1000);
myTimer.addEventListener("timer", timedFunction);
myTimer.start();

function timedFunction(eventArgs:TimerEvent) {
        var tc:int= 31 - myTimer.currentCount;
        pTime.text = tc.toString();
        if (myTimer.currentCount > 30) {
            for (var k:Number = 0; k < numClips; k++) {
                myClip[k].removeEventListener("mouseDown", pieceMove);
                myClip[k].removeEventListener("mouseUp", pieceMove);
            }
            myTimer.reset();
            myTimer.stop();
            scoreWindow.visible = true;
            scoreWindowText.visible = true;
            addChild(scoreWindow);
            addChild(scoreWindowText);
            scoreWindowText.text = "Congratulations. You got " + upgameScore + " / 10. \nClick FINISHED to go back or TRY AGAIN to restart.";
            finBtn.visible = true;
            finBtn.addEventListener(MouseEvent.CLICK, finished);
            addChild(finBtn);
            tryBtn.visible = true;
            tryBtn.addEventListener(MouseEvent.CLICK, tryAgain);
            addChild(tryBtn);
        }
    }

var mySound:Sound = new correctSound(); 

upgameScore = 0;

var numClips:Number = 7;

var myClip = new Array(numClips);

myClip[0] = addChild(new a0());
myClip[1] = addChild(new a1());
myClip[2] = addChild(new a2());
myClip[3] = addChild(new a3());
myClip[4] = addChild(new a4());
myClip[5] = addChild(new a5());
myClip[6] = addChild(new a6());
//myClip[7] = addChild(new a7());
//myClip[8] = addChild(new a8());
//myClip[9] = addChild(new a9());

myClip[0].name = "piece0";
myClip[1].name = "piece1";
myClip[2].name = "piece2";
myClip[3].name = "piece3";
myClip[4].name = "piece4";
myClip[5].name = "piece5";
myClip[6].name = "piece6";
//myClip[7].name = "piece7";
//myClip[8].name = "piece8";
//myClip[9].name = "piece9";

var nph = new Array(numClips);

nph[0] = nph0_mc;
nph[1] = nph1_mc;
nph[2] = nph2_mc;
nph[3] = nph3_mc;
nph[4] = nph4_mc;
nph[5] = nph5_mc;
nph[6] = nph6_mc;
//nph[7] = nph7_mc;
//nph[8] = nph8_mc;
//nph[9] = nph9_mc;

var tpg = new Array(numClips);

tpg[0] = tpg0_mc;
tpg[1] = tpg1_mc;
tpg[2] = tpg2_mc;
tpg[3] = tpg3_mc;
tpg[4] = tpg4_mc;
tpg[5] = tpg5_mc;
tpg[6] = tpg6_mc;
//tpg[7] = tpg7_mc;
//tpg[8] = tpg8_mc;
//tpg[9] = tpg9_mc;

var x0 = myClip[0].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y0 = myClip[0].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
var x1 = myClip[1].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y1 = myClip[1].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
var x2 = myClip[2].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y2 = myClip[2].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
var x3 = myClip[3].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y3 = myClip[3].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
var x4 = myClip[4].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y4 = myClip[4].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
var x5 = myClip[5].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y5 = myClip[5].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
var x6 = myClip[6].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y6 = myClip[6].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
/*var x7 = myClip[7].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y7 = myClip[7].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
var x8 = myClip[8].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y8 = myClip[8].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
var x9 = myClip[9].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y9 = myClip[9].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;*/

var j:Number;

for (var k:Number = 0; k < numClips; k++) {
    myClip[k].addEventListener("mouseDown", pieceMove);
    myClip[k].addEventListener("mouseUp", pieceMove);
}

function pieceMove(evt:Event):void {
    if (evt.type == "mouseDown") {
        //mySound.play();
        evt.target.startDrag();
    }
    else if (evt.type == "mouseUp") {
        //mySound.play();
        evt.target.stopDrag();

for (j = 0; j < numClips; j++) {
    if (evt.target.name == "piece" + j && 
        evt.target.hitTestObject(nph[j]) == true) {
            removeChild(myClip[j]);
            nph[j].alpha = 0;
            tpg[j].alpha = 100;
            if (j == 2) {
                setChildIndex(tpg[j], 1);
            }

            upgameScore++;
        }
    else if (evt.target.name == "piece" + j) {
        evt.target.x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
        evt.target.y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
    }
}

scor.text = upgameScore.toString();

if (upgameScore == 10) {
    msgbox.text = "Congratulations !";
    for (var k:Number = 0; k < numClips; k++) {
        myClip[k].removeEventListener("mouseDown", pieceMove);
        myClip[k].removeEventListener("mouseUp", pieceMove);
    }
    myTimer.reset();
    myTimer.stop();
    scoreWindow.visible = true;
    scoreWindowText.visible = true;
    addChild(scoreWindow);
    addChild(scoreWindowText);
    scoreWindowText.text = "Congratulations. You got " + upgameScore + " / 10. \nClick FINISHED to go back or TRY AGAIN to restart.";

}

}
}

我应该提一下,如果你看一下我执行testHitObject的代码的末尾,然后再调用removeChild,那么特定的删除工作并从框架中删除对象。

1 个答案:

答案 0 :(得分:0)

解决了这个问题。在发布这些内容之前,我应该花一点时间。

事实证明,当对象匹配时,它们将根据正在运行的removeChild()函数被删除。我当时正在做的是迭代数组并尝试删除一些已经删除的对象。所以我所做的是保持一个与对象匹配的数组,当它们被删除时,将标志更改为0.然后在最后,遍历新数组,如果有1,则从数组中删除子对象指数。如果有0,则忽略它。现在它有效。