我在 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();