我正在创建一个交互式应用程序,允许用户创建与不同颜色的圆圈碰撞的圆圈,以产生由其大小决定的声音。使用从以前的问题和 AS3 游戏教程中引用的代码,我得到了我想要工作的基础,但是 Flash 在运行时不断地喷出这个错误:
*TypeError:错误 #1009:无法访问空对象引用的属性或方法。在 proto_fla::MainTimeline/movearray()*
乱用代码并不能消除错误,坦率地说,我被卡住了。我知道示例中的 for 循环摆脱了它,但这不是我的目标,因为它一次释放多个球,而不是一个。
这是到目前为止的代码:
/*references code from
http://stackoverflow.com/questions/9953891/random-bouncing-balls-in-flash-as3-updated
http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-2/
*/
var j;
import flash.geom.Point;
import flash.events.MouseEvent;
var initialVelX = Math.random() * 5; //initial X velocity for each ball
var initialVelY = Math.random() * 5; //initial X velocity for each ball
var numBalls=100;
var arrayBalls:Array = new Array(); //makes array to put balls in
var arrayVels:Array = new Array(); //makes array for each ball's velocities
stage.addEventListener(MouseEvent.CLICK, addBall);
var count = 0;
function addBall(e:MouseEvent):void {
stage.addEventListener(Event.ENTER_FRAME, movearray);
var initialVelX = Math.random() * 5; //initial X velocity for each ball
var initialVelY = Math.random() * 5; //initial X velocity for each ball
var ball:Ball = new Ball();
ball.x = mouseX; //places ball on a random part of the stage
ball.y = mouseY; //places ball on a random part of the stage
addChild(ball); //creates a ball
arrayBalls.push(ball); //puts ball into array
var vel:Point = new Point(initialVelX,initialVelY); //creates a point determined by the velocity of ball's X and Y speed
arrayVels.push(vel); //pushes velocities of balls into array
}
function movearray(e:Event):void
{
var ball:Ball;
var vel:Point;
for (var j:uint = 0; j < numBalls; j++)
{
ball = arrayBalls[j]; //equates ball variable to balls stored in array
vel = arrayVels[j]; //equates velocity variable to velocities stored in array
ball.x += vel.x; //speed of ball determined by vel variable
ball.y += vel.y; //speed of ball determined by vel variable
if(ball.x >= stage.stageWidth-ball.width){
vel.x *= -1;
}
if(ball.x <= 0){
vel.x *= -1;
}
if(ball.y >= stage.stageHeight-ball.height){
vel.y *= -1;
}
if(ball.y <= 0){
vel.y *= -1;
}
}
}