0

我在 AS3 中的游戏出现错误。当我的数组长度为 0 时,应用程序应该进入菜单场景。但是,它总是出现一个框,上面写着:TypeError: Error #2007: Parameter child must be non-null。在 flash.display::DisplayObjectContainer/removeChild() at kitkat_game_fla::MainTimeline/moveBall()[kitkat_game_fla.MainTimeline::frame2:105]

我点击全部关闭,当我再次开始游戏时,球完全快了。我应该怎么办。是因为我的 mcBall.x 是空的,它不应该吗?我已经搜索过,没有任何效果。请帮忙。

这是我的代码

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();
4

1 回答 1

2

myArray.splice(mc2, 1);是一种错误的方法,因为第一个参数splice()是一个索引,而不是一个对象。您应该改为查询indexOf()该对象,如果它已经不在数组中,则不要拼接也不要调用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;
    }

这种方法也违背了数组的使用。通常,您不会查询单个 MC,而是遍历数组并hitTestObject针对数组中的每个项目运行。像这样:

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"); }

另外,请摆脱“场景”,它们已被弃用,可能会导致奇怪的问题。stop()相反,使用一个场景并通过时间线调用将其拆分为帧序列。

于 2013-10-18T12:29:04.187 回答