删除影片剪辑 as3

发布于 2024-08-21 12:21:15 字数 1066 浏览 5 评论 0原文

好吧,过去几个小时这让我抓狂,我知道有一个简单的答案。

我有一个scrollPane,其中有一个名为right_container_mc 的影片剪辑作为其源。 在这个 right_container_mc 中​​,我还有其他名为 execiseBox 的影片剪辑,它们是从带有 for 循环的数组中添加的(在舞台上的正确位置)。 每个练习框都有一个名为 close_btn 的按钮符号。

首先,我不确定这是实现这一目标的最佳方法,所以请随意提出更好的方法!

我想要做的是,当单击此 close_btn 时,从数组和舞台中删除特定的锻炼盒影片剪辑,然后再次循环数组,以便所有练习盒影片剪辑更新其在舞台上的位置。

我无法获取对影片剪辑的引用,因为它是嵌套的以将其从数组和舞台中删除。这是我到目前为止的代码,需要添加删除和更新部分。另外,我是否应该在每次运行数组循环之前删除exerciseBox 影片剪辑的所有当前实例?

非常感谢任何帮助。

 function addMovieClipsToStage(event:MouseEvent):void
   {
    scrollPaneRight.source = right_container_mc;
    exerciseBox = new Exercisebox();
    exerciseBox.close_btn.addEventListener(MouseEvent.CLICK, onRemoveBox);
    boxArray.push(exerciseBox);
    sortBoxes();
    scrollPaneRight.update();
   }

    function onRemoveBox(event:MouseEvent):void
   {

   }

   function sortBoxes():void
   {
    for (var i:int =0; i<boxArray.length; i++)
    {
     right_container_mc.addChild(exerciseBox);
     exerciseBox.x = 0;
     exerciseBox.y = ((115 + 3)*i);

    }

   }

Ok this has been driving me nuts for the past couple of hours and i know there is an easy answer.

I have a scrollPane which has a movie clip called right_container_mc as it's source.
Inside this right_container_mc I have other other movie clips called execiseBox that get added (in the correct positions on the stage) from an array with a for loop.
Each exercise box has a button symbol called close_btn.

Firstly I'm not sure that this is the best way to achieve this so feel free to suggest a better way!

What I want to do is when this close_btn is clicked remove the specific exerciseBox movieclip from the array and from the stage then loop through the array again so all of the exercise box movieclips update their position on the stage.

I am having trouble getting a reference to the movie clip because it is nested to remove it from the array and stage. Here is the code I have so far, need to add in the removing and updating parts. Also is should I be removing all of the current instances of the exerciseBox movie clips before the array loop runs each time??

Any help is greatly appreciated.

 function addMovieClipsToStage(event:MouseEvent):void
   {
    scrollPaneRight.source = right_container_mc;
    exerciseBox = new Exercisebox();
    exerciseBox.close_btn.addEventListener(MouseEvent.CLICK, onRemoveBox);
    boxArray.push(exerciseBox);
    sortBoxes();
    scrollPaneRight.update();
   }

    function onRemoveBox(event:MouseEvent):void
   {

   }

   function sortBoxes():void
   {
    for (var i:int =0; i<boxArray.length; i++)
    {
     right_container_mc.addChild(exerciseBox);
     exerciseBox.x = 0;
     exerciseBox.y = ((115 + 3)*i);

    }

   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖心男生 2024-08-28 12:21:15

在 onRemoveBox 函数

event.currentTarget 中应该返回触发该函数的对象。

鉴于该对象是 right_container_mc 的子对象,也许您可​​以尝试:

right_container_mc.removeChild(event.currentTarget);

根据您发布的代码,我不确定您是否需要一个数组。看起来您正在使用它来跟踪孩子的数量。显示列表已经为您做到了这一点。

所以我认为你的排序可以只引用right_container_mc.numChildren而不是数组长度。

希望其中一些有所帮助!

in your onRemoveBox function

event.currentTarget should return the object that function was triggered by.

Seeing as that object is a child of right_container_mc maybe you could try:

right_container_mc.removeChild(event.currentTarget);

Based upon the code you have posted I'm not sure you even need an array. It looks like you're using it to keep track of the number of children. Display list already does this for you.

So I think your sort could just reference right_container_mc.numChildren instead of an array length.

Hope some of this helps!

琉璃繁缕 2024-08-28 12:21:15

为了摆脱 boxArray,您可以循环遍历 right_container_mc 中​​的所有练习框。

function addMovieClipsToStage(event:MouseEvent):void
{
    scrollPaneRight.source = right_container_mc;
    exerciseBox = new Exercisebox();
    exerciseBox.close_btn.addEventListener(MouseEvent.CLICK, onRemoveBox);
    right_container_mc.addChild(exerciseBox);
    sortBoxes();
    scrollPaneRight.update();
}

function onRemoveBox(event:MouseEvent):void
{
    right_container_mc.removeChild(event.currentTarget);
    sortBoxes();
}

function sortBoxes():void
{
    var count:int = 0;
    for each(var exerciseBox:Exercisebox in right_container_mc)
    {
        count++;
        exerciseBox.x = 0;
        exerciseBox.y = (115 + 3) * count;
    }
}

有关“for every ... in”的更多信息,请查看 http://help.adobe.com/en_US/AS3LCR/Flash_10.0/statements.html#for_each..in

In order to get rid of the boxArray, you could just loop through all the ExerciseBoxes in right_container_mc.

function addMovieClipsToStage(event:MouseEvent):void
{
    scrollPaneRight.source = right_container_mc;
    exerciseBox = new Exercisebox();
    exerciseBox.close_btn.addEventListener(MouseEvent.CLICK, onRemoveBox);
    right_container_mc.addChild(exerciseBox);
    sortBoxes();
    scrollPaneRight.update();
}

function onRemoveBox(event:MouseEvent):void
{
    right_container_mc.removeChild(event.currentTarget);
    sortBoxes();
}

function sortBoxes():void
{
    var count:int = 0;
    for each(var exerciseBox:Exercisebox in right_container_mc)
    {
        count++;
        exerciseBox.x = 0;
        exerciseBox.y = (115 + 3) * count;
    }
}

For more information on 'for each ... in', check out http://help.adobe.com/en_US/AS3LCR/Flash_10.0/statements.html#for_each..in

所谓喜欢 2024-08-28 12:21:15

由于某种原因,sortBoxes 函数中的 for every 循环未触发。我添加了一条跟踪语句来检查这一点,但没有任何反应,这是更新后的代码:

public function addMovieClipsToStage(event:MouseEvent):void
            {
                scrollPaneRight.source = right_container_mc;
                exerciseBox = new Exercisebox();
                exerciseBox.close_btn.addEventListener(MouseEvent.CLICK, onRemoveBox);
                exerciseBox.x = 0;
                exerciseBox.y = (118 * exerciseBoxAmt);
                right_container_mc.addChild(exerciseBox);
                exerciseBoxAmt++; // the position of the boxes added to stage variable
                sortBoxes();
                scrollPaneRight.update();
            }

            public function sortBoxes():void
            {
                var count:int = 0;
                for each (var exerciseBox:Exercisebox in right_container_mc)
                {

                    exerciseBox.x = 0;
                    exerciseBox.y = (118 * count);
                    count++;
                    trace(count); //does not display in output window!!
                }
            }


            public function onRemoveBox(event:MouseEvent):void
            {
                right_container_mc.removeChild(event.currentTarget.parent);
                exerciseBoxAmt--;
                sortBoxes();

            }

For some reason the for each loop in the sortBoxes function is not firing. I have added a trace statement to check this and nothing happens, here is the updated code:

public function addMovieClipsToStage(event:MouseEvent):void
            {
                scrollPaneRight.source = right_container_mc;
                exerciseBox = new Exercisebox();
                exerciseBox.close_btn.addEventListener(MouseEvent.CLICK, onRemoveBox);
                exerciseBox.x = 0;
                exerciseBox.y = (118 * exerciseBoxAmt);
                right_container_mc.addChild(exerciseBox);
                exerciseBoxAmt++; // the position of the boxes added to stage variable
                sortBoxes();
                scrollPaneRight.update();
            }

            public function sortBoxes():void
            {
                var count:int = 0;
                for each (var exerciseBox:Exercisebox in right_container_mc)
                {

                    exerciseBox.x = 0;
                    exerciseBox.y = (118 * count);
                    count++;
                    trace(count); //does not display in output window!!
                }
            }


            public function onRemoveBox(event:MouseEvent):void
            {
                right_container_mc.removeChild(event.currentTarget.parent);
                exerciseBoxAmt--;
                sortBoxes();

            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文