AS3:TypeError:带有交互式应用程序的错误#1009

时间:2013-10-19 19:13:13

标签: actionscript-3 flash interactive

我正在创建一个交互式应用程序,允许用户创建与不同颜色的圆碰撞的圆圈,以产生由其大小决定的声音。使用上一个问题引用的代码和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;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

你的问题是在movearray()方法中,你是从0循环到numBalls(设置为100)。当它运行时,尚未创建100个球。

因此,错误是Flash告诉您正在尝试更改不存在的对象的属性(ball.x += vel.x;)。

要解决此问题,请尝试将movearray()中的for循环更改为:

for (var j:uint = 0; j < arrayBalls.length; j++)

这只会循环遍历arrayBalls数组中存在的球对象数。

相关问题