سؤال

سعيد قبل هالوين الجميع :)

مشكلتي اليوم هي DisplayObject خطأ أنا على الحصول عند إزالة جسم الطفل.لدي التعليمات البرمجية التي سيتم إطلاق(addChild) فيديو حاوية تحكم الفيديو وكذلك إضافة زر إغلاق. الآن الزر إغلاق يعمل كل شيء على ما يرام, إزالة الفيديو و الضوابط و أنا قادرة على اختيار آخر الفيديو مرة أخرى ، ولكن عند النقر فوق إغلاق 2nd الوقت يمكنني الحصول على هذا الخطأ:

ArgumentError:خطأ #2025:الموردة DisplayObject يجب أن يكون الطفل من المتصل.في فلاش.عرض::DisplayObjectContainer/removeChild()

حتى لقد ضاقت المشكلة أين يمكنني إزالة videoContainer (الذي يحمل كائن فيديو)

بلدي رمز لتشغيل ملفات الفيديو:

public function videoSwitch(videoName):void
{
    nv.closeOut();
    nv.resetNav = false;

    if (!videoPlaying)
    {
        vc = new VideoClass(videoName, videoHolder);
        vc.addEventListener("KillMovie", removePlayer);
        container.addChild(videoContainer);
        container.addChild(vc);
        //container.addChildAt(videoContainer, 1);
        //container.addChildAt(vc, 2);
        videoPlaying = true;
        closeVideo();
    }

    else if (videoPlaying)
    {
        vc.clearSource();
        container.removeChild(videoContainer);
        container.removeChild(vc);

        vc = new VideoClass(videoName, videoHolder);
        vc.addEventListener("KillMovie", removePlayer);
        container.addChild(videoContainer);
        container.addChild(vc);
        //container.addChildAt(videoContainer, 1);
        //container.addChildAt(vc, 2);
        closeVideo();
    }
        trace("videoPlaying = "+videoPlaying+"\r");
}

مقربة مشغل الفيديو كود: يمكنك أن ترى في تعليقاتي الأخرى رمز حاولت ولكن لا يزال الحصول على الخطأ.

function closeVideo():void 
{
    closeBtn.visible = true;
    closeBtn.x = 770;
    closeBtn.y = 20;
    closeBtn.buttonMode = true;
    container.addChild(closeBtn);
    closeBtn.addEventListener(MouseEvent.MOUSE_UP, closeButtonClicked);

    function closeButtonClicked(event:MouseEvent):void 
    {
        vc.clearSource();
        container.removeChild(videoContainer);
        //container.removeChildAt(videoContainer, 1);
        container.removeChild(vc);
        videoPlaying = false;
        closeBtn.visible = false;
    }
}

الآن فيلمي يعمل بشكل جيد, ولكن أخشى أن هذا الخطأ يحدث في الخلفية (و يظهر في نافذة الإخراج) في نهاية المطاف سوف يسبب مشكلة في مكان آخر :(

شكرا مقدما على أي عيون على هذا واحد!:)


تحديث: الثابتة! المشكلة انا إزالة قتل VC المستمع ، ولكن نسيت لإزالة غبي زر إغلاق Mouse_Event المستمع :(

function addCloseButton():void 
{
    container.addChild(closeBtn);
    closeBtn.addEventListener(MouseEvent.MOUSE_UP, closeButtonClicked);

    function closeButtonClicked(event:MouseEvent):void 
    {
        videoPlaying=false;
        vc.clearSource();
        removeContainerChildren(); // <- thx Joel!
        closeBtn.removeEventListener(MouseEvent.MOUSE_UP, closeButtonClicked);
        //^ Forgot this line - thx Jotham!
        container.removeChild(closeBtn);
    }
}

لا أعرف إذا كان هذا الرسم يساعد ولكن:alt text

هل كانت مفيدة؟

المحلول

هل إزالة المستمع?هل يمكن أن يكون جيدا بعد ذلك النار عدة مرات.

نصائح أخرى

هنا هو نهج واحد لتجنب الخطأ:

    public function videoSwitch(videoName):void
    {
        nv.closeOut();
        nv.resetNav = false;

        if (videoPlaying)
        {
            vc.clearSource();
            removeContainerChildren()
        }

        addContainerChildren();
        closeVideo();
    }

    protected function removeContainerChildren():void
    {
        if(container.contains(videoContainer))
            container.removeChild(videoContainer);
        if(container.contains(vc))
        {
            container.removeChild(vc)   
            vc.removeEventListener("KillMovie", removePlayer)
        }
    }

    protected function addContainerChildren():void
    {
        videoPlaying = true;
        vc = new VideoClass(videoName, videoHolder);
        vc.addEventListener("KillMovie", removePlayer, false, 0, true); 
        container.addChild(videoContainer);
        container.addChild(vc);

        trace("videoPlaying = "+videoPlaying+"\r");
    }

جرب هذا:

 container.removeChild(container.videoContainer);
 container.removeChild(container.vc);

لدي شعور أن بعض قطعة أخرى من التعليمات البرمجية تسبب المشكلة الفعلية.هذا الخطأ من شأنه أن يجعل الشعور إذا videuPlaying متغير تغير مكان آخر بحيث كنت إزالة شيئا لم يكن موجودا حتى الآن.ربما تأكد من عدم تغيير هذا المتغير في مكان آخر.

هذا هو آخر اوبر hacky طريقة للقيام بذلك, لا ينصح عادة ولكن سوف بالتأكيد ضمان أن videoContainer/vc هو إزالتها من أيهما DisplayList هو على.

private function removeFromStack(target:DisplayObject):void
{
    if (target.parent)
        target.parent.removeChild(target);
}

private function removeVideo():void
{
    removeFromStack(vc);
    removeFromStack(videoContainer);
    vc = videoContainer = null;
}

فقط إلى إعادة itterate هذه ليست الطريقة المفضلة ، ولكنها سوف تعمل دون أخطاء.إذا كنت تبدأ في الحصول على "لا يمكن الوصول إلى null مرجع كائن" خطأ ، ثم كما في السابق الناس قد اقترح الحدث المستمعين أو بعض الأخرى dependancies لا تزال تحتفظ بها DisplayObject's في السؤال.

ويساعد هذا الأمل

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top