0
votes

I am making a minions game. My minion's instance name is sideMinion. The bananas are falling and everything works fine, but my removeChild() doesn't work properly. The error I'm getting is

Error #2025: The supplied DisplayObject must be a child of the caller. The removeChild() or the hitTestObject doesn't work properly.

This is what I have in my banana as. class:

    package {
    import flash.display.MovieClip;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.events.Event;

    public class banana extends MovieClip {

        var velX: Number = 0;
        var velY: Number = 0;
        var falling: Boolean = false;
        var gravity: Number = 0;

        public function banana() {
            var timing: Timer = new Timer(25, 0);
            timing.addEventListener(TimerEvent.TIMER, moveMe);
            timing.start();

        }

         private function moveMe(event: TimerEvent)
    {
        x += velX;
        y += velY;

        if (falling) velY += gravity;

       /* trace("[BANANA] position:", x, y, "speed:", velX, velY);*/
    }


        public function setSpeed(dx,dy) {
            velX = dx;
            velY = dy;
        }

            public function setSpot(atX,atY){
            this.x=atX;
            this.y=atY;
        }


        public function makeItFall (){
            falling=true;
        }


    }

}

And in my main program:

import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.events.Event;

var leftKey:Boolean;
var rightKey:Boolean;
var upKey:Boolean;
var downKey:Boolean;
var jump:Boolean = false;
var xvelocity:int = 9;
var yvelocity:int = 3;
var gravity:Number = 7;


stage.addEventListener(Event.ENTER_FRAME, changeVelocity);
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeyUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeyDown);

  function changeVelocity(evt:Event){
        moveMinion();
        yvelocity += gravity;
    }

    function moveMinion(){

        if (leftKey == true){
           sideMinion.x -= xvelocity;
            sideMinion.left();

        }
        if (rightKey == true){
            sideMinion.x += xvelocity;
            sideMinion.right();
        }

    }

    function checkKeyDown(e:KeyboardEvent){
        if (e.keyCode == Keyboard.LEFT){
            leftKey = true;
        }
        else if (e.keyCode == Keyboard.RIGHT){
            rightKey = true;
        }

    }

    function checkKeyUp(e:KeyboardEvent){
        if (e.keyCode == Keyboard.LEFT){
            leftKey = false;
        }
        else if (e.keyCode == Keyboard.RIGHT){
            rightKey = false;
        }

    }

    btnStart.addEventListener(MouseEvent.CLICK, makeItFall);

    function makeItFall(e:MouseEvent){



    var numBananas = 6;
    var theBananas: Array = new Array();
    theBananas = [];
    for (var i = 0; i < numBananas; i++) {
    var aBanana: banana = new banana();
    theBananas.push(aBanana);
    btnStart.visible=false;
    aBanana.y=30;
    theBananas[i].setSpot(Math.random()*450+50,Math.random()*200+20);
    theBananas[i].setSpeed((Math.random()), 1);
    stage.addChild(aBanana);

}   

var health: uint= 1;

addEventListener(Event.ENTER_FRAME, pickUpBananas);

function pickUpBananas(event:Event){
    for( var i= 0; i<theBananas.length; ++i){
        if (sideMinion.hitTestObject(theBananas[i])){
            removeChild(theBananas[i]);
            health=health+1;
            trace(health);
        }
    }

}

    }


stop();

Edit : format code

1
Since you stage.addChild(...try removeChild also for stage: stage.removeChild(.... But, you probably also want to remove deleted bananas from theBananas -array? If so, include that into the pickUp-loop. In that case I would then also turn the loop to run backwards for(i=theBananas.length-1; i>-1; i--)kaarto

1 Answers

1
votes

As you are adding child to stage, you'll have to remove it also from stage:

stage.removeChild(theBananas[i]);

For future, in some situations you can also use parent property if you don't know the actual parent:

theBananas[i].parent.removeChild(theBananas[i]);

In your game, I assume you want also to remove the bananas from theBananas-array when you remove bananas from stage, so that your array wont end up having already deleted bananas. So, here's couple modifications:

for(var i:int = theBananas.length-1; i>-1; i--){ //inverted loop
    if (sideMinion.hitTestObject(theBananas[i])){
        stage.removeChild(theBananas[i]);
        theBananas.splice(i,1); //removing it from the array
        health=health+1;
        trace(health);
    }
}

Inverted loop obviously loops from last element all the way to the first one, becouse if you'd remove the first element from the array, then second element would 'jump' into its place and loop would skip it.

I hope we all will soon get to see your game! :)