问答详情
源自:5-2 屏幕自适应

小球重叠问题

离开页面后,再回来会发现有很多小球都叠在一起了

提问者:请叫我橙子蛋 2016-08-21 14:44

个回答

  • 慕粉1470740819
    2016-08-25 17:17:18
    已采纳

    性能优化function updateBalls(){

        for( var i = 0 ; i < balls.length ; i ++ ){

            balls[i].x += balls[i].vx;
            balls[i].y += balls[i].vy;
            balls[i].vy += balls[i].g;

            if( balls[i].y >= WINDOW_HEIGHT-RADIUS ){
                balls[i].y = WINDOW_HEIGHT-RADIUS;
                balls[i].vy = - balls[i].vy*0.75;
            }
        }

        var cnt = 0
        for( var i = 0 ; i < balls.length ; i ++ )
            if( balls[i].x + RADIUS > 0 && balls[i].x -RADIUS < WINDOW_WIDTH )
                balls[cnt++] = balls[i]

        while( balls.length > Math.min(300,cnt) ){
            balls.pop();
        }
    }