清除DisplayObject()而不提供对象名称

时间:2019-03-18 03:44:54

标签: actionscript-3 flash

我试图做一个Flash应用程序,当单击第一个按钮时,displayObject函数将显示图像。单击第二个按钮时,前一个持有人需要清除其中的所有对象,这是我所坚持的部分。有解决办法吗?

function showOppo (e:Event):void
{
    if(holder_1.stage)
    {
    holder_1 = new MovieClip();
    holder_2 = new MovieClip();
    holder_3 = new MovieClip();
    }
    holder_1.x = 37.6;
    holder_1.y = 133.05;
    holder_2.x = 458.55;
    holder_2.y = 133.05;

    Of9.scaleX = 1;
    Of9.scaleY = 1;
    Ofx.scaleX = 1;
    Ofx.scaleY = 1;

    holder_1.addChild(Of9);
    holder_2.addChild(Ofx);

    stage.addChild(holder_1);
    stage.addChild(holder_2);

    stage.setChildIndex(holder_1,1);
    stage.setChildIndex(holder_2,1);
}
function showsamsung (e:Event):void
{
    if(holder_1.stage)
    {
    holder_1 = new MovieClip();
    holder_2 = new MovieClip();
    holder_3 = new MovieClip();
    }
    holder_1.x = 37.6;
    holder_1.y = 133.05;
    holder_2.x = 458.55;
    holder_2.y = 133.05;

    Sgn9.scaleX = 1;
    Sgn9.scaleY = 1;
    Ss10.scaleX = 1;
    Ss10.scaleY = 1;

    holder_1.addChild(Sgn9);
    holder_2.addChild(Ss10);

    stage.addChild(holder_1);
    stage.addChild(holder_2);

    stage.setChildIndex(holder_1,1);
    stage.setChildIndex(holder_2,1);
}

在代码中,我使用new MovieClip(),但是单击第二个按钮时什么也没有发生。单击第二个按钮后,我返回并再次单击第一个按钮,显示第二个功能。

1 个答案:

答案 0 :(得分:2)

  

“我尝试找出如何删除每个持有人的现有子级,因为如果没有,所有这些图像都将堆叠在一起”

是的,当您addChild时,就像告诉Flash "paste this on a new layer inside the target" 。每个层称为深度级别。您可以使用getChildAt(x)来访问特定的层/深度,其中x是层号(first = 0,second = {1,third = 2等..)

示例:
假设您有一位持有人( eg: holder_1)用于显示所有图像。

  • 1)第一次使用holder_1.addChild(pic1);为孩子添加图片
    因此,pic1现在位于holder_1目标内部(第0层)层。

  • 2)使用holder_1.addChild(pic2);添加第二张图片
    因此,pic2现在位于holder_1目标内部(第1层)层。

现在,将两个图像堆叠在同一容器内的不同层上。怎么修?考虑以下逻辑:

holder_1.addChild(pic2); //add pic2 into holder (at level 0)
holder_1.removeChildAt(0); //remove anything at level 0 (...is pic2)
holder_1.addChild(pic1); //add different pic1 into holder (at level 0)

您的代码可以尝试执行以下操作:

holder_1 = new MovieClip();
holder_2 = new MovieClip();
holder_3 = new MovieClip();

holder_1.x = 37.6;  holder_2.x = 458.55;

//# give multiple VARS the same value   
holder_1.y = holder_2.y = 133.05;

stage.addChild(holder_1);
stage.addChild(holder_2);

btnoppo.addEventListener(MouseEvent.CLICK,showOppo);
btnsamsung.addEventListener(MouseEvent.CLICK,showsamsung);

function showOppo (e:Event):void
{
    //# set multiple at once to value = 1
    Of9.scaleX = Of9.scaleY = Ofx.scaleX = Ofx.scaleY = 1;

    //# check if already has image & remove it
    if(holder_1.getChildAt(0) != null) { holder_1.removeChildAt(0); }
    if(holder_2.getChildAt(0) != null) { holder_2.removeChildAt(0); }

    holder_1.addChild(Of9); holder_2.addChild(Ofx);

}
function showsamsung (e:Event):void
{
    Sgn9.scaleX = Sgn9.scaleY = Ss10.scaleX = Ss10.scaleY = 1;

    //# check if already has image & remove it
    if(holder_1.getChildAt(0) != null) { holder_1.removeChildAt(0); }
    if(holder_2.getChildAt(0) != null) { holder_2.removeChildAt(0); }

    holder_1.addChild(Sgn9); holder_2.addChild(Ss10);

}