首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Flash CS3删除舞台上的对象

Flash CS3删除舞台上的对象
EN

Stack Overflow用户
提问于 2014-11-27 00:59:00
回答 1查看 47关注 0票数 0

好的,这个小游戏在我的主时间线里。小型游戏使用addChild(新a0)、新a1、新a2等工具动态地在数组中创建一组对象。无论如何,在游戏结束时,有一个选项可以重新启动(重置分数并返回开始帧)或完成(返回几帧到“主屏幕”,该屏幕位于另一层,并返回几个帧)。如果我选择任何一个选项,任何没有删除的对象,从游戏中删除(得到一个匹配)留在舞台上,即使是重新开始或回到主框架。我尝试过各种方法来调用removeChild,将数组设置为空,什么不是,但我似乎找不出如何删除它们。在这里显示的代码中,我得到了以下错误:

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

这是代码

代码语言:javascript
复制
    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,则该特定的delete工作并从框架中删除该对象。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-27 03:13:18

这个问题也解决了。我可能应该花更多的时间,然后我发布这些。

事实证明,当对象被匹配时,它们将按照正在工作的removeChild()函数被移除。当时我所做的是遍历数组并尝试删除一些已经删除的对象。因此,我所做的是保留一个与对象匹配的数组,当对象被移除时,将一个标志更改为0。最后,迭代新数组,如果有1,则使用相同的索引从数组中删除子对象。如果有0,忽略它。现在起作用了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27161472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档