actionscript-3flash-cs6

Can you tell me my fault? it's about if nested (flash cs6 as3)


I've already checked but still get wrong? this is the comment :

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. at flash.display::DisplayObjectContainer/removeChild() at gamebalapanoret_fla::MainTimeline/batas()

I want make if nested

when score amount multiple of 2 then make var jwbn.and than jwbn remove when hittest batas_mc. although i trough removelistener it doesn't work. but I get the error when I put removelistener. can you tell me where is my fault? this is my code:


//membuat object batu
var Batumc:batu_mc = new batu_mc(); // creates a instance of the movieclip, i.e, an object
var Batumc1:batu_mc = new batu_mc();

        addChild(Batumc);       
        addChild(Batumc1);


        Batumc.x = 350;
        Batumc.y = 50;

        Batumc1.x = 500;
        Batumc1.y = 50;

//batas batu
var BatasBatu = Batumc.width/12;


rumput1_mc.addEventListener(Event.ENTER_FRAME, rumput);
    function rumput(e:Event):void{
        //batas gerak pemain
        while (rumput1_mc.hitTestPoint(pemain.x+jarakKanan, pemain.y, true)) {
            pemain.x++;
        }
        while (rumput1_mc.hitTestPoint(pemain.x-jarakKiri, pemain.y, true)) {
            pemain.x--;
        }
        jwbn.y += kecepatanJawab;
    }


//var jwbn
var jwbn:jwb = new jwb();
var BatasJwb = jwbn.width/6;



batas_mc.addEventListener(Event.ENTER_FRAME, batas);
    function batas(e:Event):void{       
        if(batas_mc.hitTestPoint(Batumc.x, Batumc.y+BatasBatu, true)) {
            //menghitung banyak tumbukan
            score += 1;
            point.text = score.toString();

            if(score % 2 == 0){         


            //model soal
            Soal_mc.angka1.text = mtk1.toString();
            Soal_mc.op.text = "+";
            Soal_mc.angka2.text = mtk2.toString();

            //soal muncul
            TweenLite.to(Soal_mc, 1, {x:350 ,y:180, alpha:1, ease:Expo.easeOut});


            //jawaban muncul
            addChild(jwbn);
            jwbn.x = 350;
            jwbn.y = 50;


            }


            removeChild(Batumc);
            removeChild(Batumc1);

            addChild(Batumc);
            addChild(Batumc1);

            Batumc.x = 350;
            Batumc.y = 50;

            Batumc1.x = 500;
            Batumc1.y = 50;


        }

        if(batas_mc.hitTestPoint(jwbn.x, jwbn.y+BatasJwb, true)) {
                removeChild(jwbn);
            }

        if(pemain.hitTestObject(Batumc)){
            Batumc.y -= kecepatanRintangan;
            pemain.gotoAndPlay("jatuh");
            Batumc1.y -= kecepatanRintangan;

            removeChild(Batumc);
            removeChild(Batumc1);
            rumput1_mc.removeEventListener(Event.ENTER_FRAME, rumput);
            pemain.removeEventListener(Event.ENTER_FRAME, eframe);
            stage.removeEventListener(KeyboardEvent.KEY_UP, buttonUp);
            stage.removeEventListener(KeyboardEvent.KEY_DOWN, buttonpress);
            batas_mc.removeEventListener(Event.ENTER_FRAME, batas);
            bt_home.removeEventListener(MouseEvent.CLICK, backhome);
            Soal_mc.removeEventListener(Event.ENTER_FRAME, soal);
            gotoAndStop("kalah","lose");

        }
        else if(pemain.hitTestObject(Batumc1)){
            Batumc.y -= kecepatanRintangan;
            pemain.gotoAndPlay("jatuh");
            Batumc1.y -= kecepatanRintangan;

            removeChild(Batumc);
            removeChild(Batumc1);
            rumput1_mc.removeEventListener(Event.ENTER_FRAME, rumput);
            pemain.removeEventListener(Event.ENTER_FRAME, eframe);
            stage.removeEventListener(KeyboardEvent.KEY_UP, buttonUp);
            stage.removeEventListener(KeyboardEvent.KEY_DOWN, buttonpress);
            batas_mc.removeEventListener(Event.ENTER_FRAME, batas);
            bt_home.removeEventListener(MouseEvent.CLICK, backhome);
            Soal_mc.removeEventListener(Event.ENTER_FRAME, soal);
            gotoAndStop("kalah","lose");            
        }

    }



Solution

  • "The supplied DisplayObject must be a child of the caller."

    Look at your code: (shortened)...

    batas_mc.addEventListener(Event.ENTER_FRAME, batas);
    
    function batas(e:Event):void
    {
        removeChild(Batumc);
    }
    

    So batas_mcis the caller of that batas function which you now tell to removeChild(Batumc); but the problem is you never said batas_mc.addChild(Batumc);. That batas_mc cannot remove something it does not have (eg: is not contained/added inside that MC).

    Possible solution: (untested)...

    Replace

    batas_mc.addEventListener(Event.ENTER_FRAME, batas);
    

    With code below (add event listener to the Stage itself not the batas_mc).

    addEventListener(Event.ENTER_FRAME, batas);
    //stage.addEventListener(Event.ENTER_FRAME, batas); //# or try this