Francisco
Francisco

Reputation: 51

AS 3 Error Null Object

I've got an error in my game in AS3. When my array lenght is 0 the app should go to the Menu scene. However it always appears a box saying: TypeError: Error #2007: Parameter child must be non-null. at flash.display::DisplayObjectContainer/removeChild() at kitkat_game_fla::MainTimeline/moveBall()[kitkat_game_fla.MainTimeline::frame2:105]

I click on dismiss all and when i start the game again the ball is completely fast. What should i do. Is it because my mcBall.x is null and it shouldn´t? I already search and nothing works. Some help please.

Here's my code

stop();

    var ballSpeedX:int = 23;//Velocidade em X da bola.
    var ballSpeedY:int = 23;//Velocidade em Y da bola.

    var mySound:Sound = new myFavSong(); 

    var myArray:Array = new Array(mc1,mc2);
    trace (myArray.length);

    function iniciarCode():void{
        mcPaddle.addEventListener(Event.ENTER_FRAME, movePaddle);
        mcBall.addEventListener(Event.ENTER_FRAME, moveBall);
    }

    function movePaddle(event:Event):void{

        var dx:int = mcPaddle.x - mouseX;
        mcPaddle.x -= dx / 5;


        if(mcPaddle.x <= mcPaddle.width/2){ 
            mcPaddle.x = mcPaddle.width/2;
        }else if(mcPaddle.x >= stage.stageWidth-mcPaddle.width/2){
            mcPaddle.x = stage.stageWidth-mcPaddle.width/2; 
        }
    }


    function moveBall(event:Event):void{

        mcBall.x += ballSpeedX;
        mcBall.y += ballSpeedY;

        if(mcBall.x <= mcBall.width/2){
            mcBall.x = mcBall.width/2;
            ballSpeedX *= -1; 

        } else if(mcBall.x >= stage.stageWidth-mcBall.width/2){
            mcBall.x = stage.stageWidth-mcBall.width/2;
            ballSpeedX *= -1;
        }

        if(mcBall.y <= mcBall.height/2){
            mcBall.y = mcBall.height/2;
            ballSpeedY *= -1;

        } else if(mcBall.y >= stage.stageHeight-mcBall.height/2){
            mcBall.y = stage.stageHeight-mcBall.height/2;
            ballSpeedY *= -1;
        }


        if(mcBall.hitTestObject(mcPaddle)){
            calcBallAngle();
        }   
        if(mcBall.hitTestObject(mc_lettering)){
            calcBallAngle();
        }   
        if(mcBall.hitTestObject(mc1)){      
            removeChild(mc1);
            myArray.splice(mc1, 1)
            trace(myArray.length);
            mc1 == null;

        }
        if(mcBall.hitTestObject(mc2)){  
            removeChild(mc2);
            myArray.splice(mc2, 1);
            trace(myArray.length);
            mc2 == null;
        }

        if(myArray.length == 0){
            gotoAndPlay(1,"Menu");
        }

        if(mcBall.hitTestObject(mcPaddle.barra_mc1)){   
            mcPaddle.removeChild(mcPaddle.barra_mc1);
            mcPaddle.barra_mc1 == null;
            mySound.play();

        }




    }

    function calcBallAngle():void{
        var ballPosition:Number = mcBall.x - mcPaddle.x;
        var hitPercent:Number = (ballPosition / (mcPaddle.width - mcBall.width)) - .5;
        ballSpeedX = hitPercent * 30;
        ballSpeedY *= -1;
    }

    iniciarCode();

Upvotes: 0

Views: 75

Answers (1)

Vesper
Vesper

Reputation: 18747

myArray.splice(mc2, 1); is a wrong approach, because the first parameter to splice() is an index, not an object. You should instead query indexOf() for that object, and if it's off the array already, you don't splice and don't call removeChild().

if (mc1) if (mc1.parent) // if mc1 is detached, why checking hit test?
    if(mcBall.hitTestObject(mc1)){      
        removeChild(mc1);
        myArray.splice(myArray.indexOf(mc1), 1) // at this point mc1 is still in array
        trace(myArray.length);
        // mc1 == null; this is wrong, unless you create a new mc1 at the start of the game
       // and it should spell one equal sign, not two.

    }
if (mc2) if (mc2.parent)
    if(mcBall.hitTestObject(mc2)){  
        removeChild(mc2);
        myArray.splice(myArray.indexOf(mc2), 1);
        trace(myArray.length);
        // mc2 == null;
    }

Also such an approach defies the use of an array. Normally you don't query the individual MCs, but instead you iterate through the array and run hitTestObject against every item in the array. Like this:

for (var i:int=myArray.length-1;i>=0;i--) {
    if (mcBall.hitTestObject(myArray[i])) {
        removeChild(myArray[i]);
        myArray.splice(i,1);
        // that's all
    }
}
if (myArray.length==0) { gotoAndPlay(1,"Menu"); }

Also, please get rid of "scenes", they are deprecated and can cause weird issues. Instead, use one scene and split it into frame sequences via timeline stop() calls.

Upvotes: 2

Related Questions