我的AS3游戏中出现错误。当我的数组长度为0时,应用程序应该转到菜单场景。然而它总是出现一个框说: TypeError:错误#2007:参数child必须为非null。 在flash.display :: DisplayObjectContainer / removeChild() 在kitkat_game_fla :: MainTimeline / moveBall()[kitkat_game_fla.MainTimeline :: frame2:105]
我点击全部取消,当我再次开始游戏时,球速度非常快。我该怎么办。是因为我的mcBall.x是null而且它不应该?我已经搜索过,没有任何作用。请帮忙。
这是我的代码
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();
答案 0 :(得分: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()
调用将其拆分为帧序列。