Flash仅删除未通过代码创建的对象

时间:2013-06-01 01:55:50

标签: actionscript-3 flash

我正在尝试在Flash中制作一个简单的游戏。到目前为止,我已经使用addChild(objName);向舞台添加了一堆对象。但是,现在我正试图完全删除这些对象。我不想循环遍历每个对象的名称,我确信必须有一种更有效的方法来选择每个对象(可能是通过舞台上的索引)和removeChildAt(index);它。但是,当我尝试这个时,Flash只会移除我在舞台上手动放置的对象。它不会删除通过代码放置的那些。我做了一些搜索,我尝试了多种方法,所有方法都产生相同的结果。大多数人都同意的是这一个:

while (numChildren > 0) 
{
   removeChildAt(0);
}

你能帮我弄清楚为什么这不会删除编码到舞台上的任何内容吗?

提前致谢:D

编辑:以下是我的代码:

第1帧(随机生成并显示点):

import flash.events.MouseEvent;
import fl.motion.easing.Linear;

var dotList = new Array(); var level:int = 3; var invisoDotList = new Array();
var loop:int;
var line:Line = new Line();
line.x = 274;
line.y = 187;
addChild(line);
for(loop = 0; loop < level; loop++)
{
    var dot:Dot = new Dot();
    var invisoDot:InvisoDot = new InvisoDot();
    var tester:Boolean = true;
    var xval:int = Math.floor(Math.random()*(1+520))+14;
    var looper:int = 0;
    while(looper < dotList.length)
    {
        if(Math.abs(xval - dotList[looper].x) > 30)//minimum spacing
        {
            looper++;
        }
        else
        {
            looper = 0;
            xval = Math.floor(Math.random()*(1+520))+14;
        }
    }
    dot.x = xval;
    dot.y = 187;
    invisoDot.x = xval;
    invisoDot.y = 187;
    invisoDot.alpha = 0;
    dotList[loop] = dot;
    invisoDotList[loop] = invisoDot;
    addChild(invisoDot);
    addChild(dot);
}
//trace(dotList); test to ensure that dots are added to the array
var nb1:NextButton = new NextButton();
nb1.x = 0;
nb1.y = 0;
nb1.alpha = 0;
addChild(nb1);
nb1.addEventListener(MouseEvent.CLICK, hideDots);

function hideDots(e:MouseEvent)
{
    for(var loop:int = 0; loop < dotList.length; loop++)
    {
        dotList[loop].alpha = 0;//make dots disappear
    }
    line.alpha = 0;
    nextFrame();
}
stop();

第2帧(显示更多说明并包含稍后将使用的几种方法):

import flash.events.MouseEvent;
removeChild(nb1);
var nb2:NextButton = new NextButton();
nb2.x = 0;
nb2.y = 0;
nb2.alpha = 0;
addChild(nb2);
nb2.addEventListener(MouseEvent.CLICK, next);

function next(e:MouseEvent)
{
    nextFrame();
}
function clearStage()
{
    while (numChildren > 0) 
    {
        trace("before" + numChildren);
        removeChildAt(0);
        trace("after" + numChildren);
    }
}
stop();

第3帧(单击它们使点消失并保持准确计数):

import flash.events.MouseEvent;
import flash.utils.Timer;
line.alpha = 1;
removeChild(nb2);
//setChildIndex(line,0);
var clicks:int = -1;
var passed:int = 0;
var fromLine:Boolean = false;
//trace(dotList.length);
stage.addEventListener(MouseEvent.CLICK, clickCount);
for(var loopvar:int = 0; loopvar < dotList.length; loopvar++)
{
    //trace("loop");
    dot = dotList[loopvar];
    invisoDot = invisoDotList[loopvar];
    dot.addEventListener(MouseEvent.CLICK, onClick);
    invisoDot.addEventListener(MouseEvent.CLICK, onClick);
    //trace("event");
}
//trace(dotList.length);
function onClick(e:MouseEvent)
{
    //e.currentTarget.alpha = .5;
    for(var hitcheck:int = 0; hitcheck < dotList.length; hitcheck++)
    {
        if(dotList[hitcheck].x == e.currentTarget.x)
        {
            dotList[hitcheck].alpha = 1;
        }
    }
    //trace("check");
}
var numChanged:int = 0;
function clickCount(e:MouseEvent)
{
    clicks++;
    //trace(clicks);
    numChanged = 0;
    for(var index:int = 0; index < dotList.length; index++)//check whether the user has gotten all the dots
    {
        if(dotList[index].alpha == 1)
        {
            numChanged++;
        }
    }
    if(numChanged == level)//if the user has gotten all the dots
    {
        /*trace("next screen for sucess");
        trace(clicks);*/
        line.visible = false;
        for(loop = 0; loop < dotList.length; loop++)
        {
            dotList[loop].alpha = 0;//make dots disappear
        }
        if((clicks - level) == 1)
        {
            passed = 2
        }
        else if((clicks - level) == 0)
        {
            passed = 1;
        }
        passed = 1;
        fromLine = true;
        nextFrame();
    }
    else if((clicks - numChanged) >= 2)//this ends the session as soon as 2 mistakes are made
    {
        /*trace("next screen for failed number of clicks");
        trace(clicks);*/
        line.visible = false;
        for(loop = 0; loop < dotList.length; loop++)
        {
            dotList[loop].alpha = 0;//make dots disappear
        }
        passed = 3;
        fromLine = true;
        nextFrame();
    }
    /*else if((clicks - level) >= 2)//if the user has made too many mistakes. This ends the session after the maximum number of tries have been used
    {
        trace("next screen too many clicks");
        trace(clicks);
    }*/

}
//trace("end");
stop();

第4帧(生成结果表。附注:有一个错误,其中“Okay”永远不会是结果,因为在第3帧中,传递的值永远不等于2.不确定为什么):

import flash.text.TextFormat;
import flash.text.TextField;

var failFormat:TextFormat = new TextFormat();
failFormat.color = 0xFF0000;
failFormat.font = "Arial";
failFormat.size = 18;
var passFormat:TextFormat = new TextFormat();
passFormat.color = 0x00FF00;
passFormat.font = "Arial";
passFormat.size = 18;
var okayFormat:TextFormat = new TextFormat();
okayFormat.color = 0x808000;
okayFormat.font = "Arial";
okayFormat.size = 18;
var normalFormat:TextFormat = new TextFormat();
normalFormat.color = 0x000000;
normalFormat.font = "Arial";
normalFormat.size = 18;

var lineResults = new Array();
var squareResults = new Array();
trace(passed);
if(fromLine == true)
{
    if(passed == 1)
    {
        lineResults[lineResults.length] = "Pass";
    }
    else if(passed == 2)
    {
        lineResults[lineResults.length] = "Okay";
    }
    else if(passed == 3)
    {
        lineResults[lineResults.length] = "Fail";
    }

}
fromLine = false;
lineResults = lineResults.reverse();
squareResults = squareResults.reverse();
var loopLength:int = (lineResults.length >= squareResults.length) ? lineResults.length : squareResults.length;
var loopStart:int = 0;
if(loopLength > 11)
{
    loopStart = loopLength - 12
}
var cb:CellBlock = new CellBlock();
cb.x = 283.05;
cb.y = 20.35;
addChild(cb);
var col1Head:TextField = new TextField();
col1Head.defaultTextFormat = normalFormat;
col1Head.text = "# of Dots";
col1Head.x = 114.95
col1Head.y = 8.3;
addChild(col1Head);
var col2Head:TextField = new TextField();
col2Head.defaultTextFormat = normalFormat;
col2Head.text = "Line";
col2Head.x = 259.95
col2Head.y = 8.3;
addChild(col2Head);
var col3Head:TextField = new TextField();
col3Head.defaultTextFormat = normalFormat;
col3Head.text = "Square";
col3Head.x = 381.95
col3Head.y = 8.3;
addChild(col3Head);

for(loop = loopStart; loop < loopLength; loop++)
{
    var block:CellBlock = new CellBlock();
    block.x = 283.05;
    block.y = 20.35 + (loop - loopStart + 1)*33;
    addChild(block);
    var col2:TextField = new TextField();
    var col3:TextField = new TextField();
    var col1:TextField = new TextField();
    /*col2.defaultTextFormat = passFormat;
    col3.defaultTextFormat = okayFormat;*/
    col1.defaultTextFormat = normalFormat;
    switch(lineResults[loop])
    {
        case "Pass":
        col2.defaultTextFormat = passFormat;
        break;
        case "Okay":
        col2.defaultTextFormat = okayFormat;
        break;
        case "Fail":
        col2.defaultTextFormat = failFormat;
        break;
    }
    switch(squareResults[loop])
    {
        case "Pass":
        col3.defaultTextFormat = passFormat;
        break;
        case "Okay":
        col3.defaultTextFormat = okayFormat;
        break;
        case "Fail":
        col3.defaultTextFormat = failFormat;
        break;
    }
    //col2.text = "Pass";
    col2.text = lineResults[loop];
    col2.x = 260.95;
    col2.y = block.y - 12;
    addChild(col2);
    //col3.text = "Okay";
    try
    {
        col3.text = squareResults[loop];
    }
    catch(e:Error)
    {

    }
    col3.x = 386.95;
    col3.y = block.y - 12;
    addChild(col3);
    col1.text = String(loop + 1);
    col1.x = 133.95;
    col1.y = block.y - 12;
    addChild(col1);
}
var nb4:NextButton = new NextButton();
nb4.x = 0;
nb4.y = 0;
nb4.alpha = 0;
addChild(nb4);
nb4.addEventListener(MouseEvent.CLICK, clearStage);
stop();

第5帧(下一帧是一个测试,以确保所有内容都被删除,但它没有):

removeChild(nb4);
stop();

2 个答案:

答案 0 :(得分:1)

此错误:

  

ArgumentError:错误#1063:Game_fla :: MainTimeline / clearStage()上的参数计数不匹配。预期0,得到1. clearStage()

因为您的clearStage()方法已添加为按钮的单击事件处理程序而发生。如果您的clearStage()函数将用作事件处理程序,则需要接受“event”参数。所以你应该定义这样的函数:

function clearStage(event:Event)
{
    while (numChildren > 0) 
    {
        trace("before" + numChildren);
        removeChildAt(0);
        trace("after" + numChildren);
    }
}

作为旁注,这意味着如果您还要手动调用clearStage(),即使用它而不将其添加为事件处理程序,则需要包含此event参数...但是您可以传入null,因为您的代码不需要实际使用event参数:

clearStage(null);

我不确定我还有其他错误。我首先将该事件参数添加到clearStage()函数中,如上所示。

我还应该补充一点,我主要使用Flex或纯AS3,并且在Flash CS6和时间轴编程方面不是很熟练:)

答案 1 :(得分:0)

从内存中,我不认为Flash会在您删除孩子时重新订购。如果删除索引0处的子项,则每个其他子项的编号仍为1到x。 childAt(0)现在只是null。请记住这种过程。