Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Random Object Generator

    ////<script type="text/javascript">

    var balls = [];

    function createRandomBalls(numOfBalls){

    for(var i=0; i<numOfBalls; i++){
    var radius = parseInt(Math.random()*10)+10;
    var color = '#'+Math.round(Math.random()*16777215).toString(16);
    var ball = new Ball(radius,color);
    ball.x = 250;
    ball.y = 250;
    ball.vx = Math.random()*4-2;
    ball.vy = Math.random()*4-2;
    balls.push(ball);

    }
    }

    createRandomBalls(10);

    function checkBoundaryCollision(ball){
    if(ball.x<ball.radius){
    ball.x = ball.radius;
    ball.vx = -ball.vx;
    }

    if(ball.y<ball.radius){
    ball.y = ball.radius;
    ball.vy = -ball.vy;

    }

    if(ball.x>500-ball.radius){
    ball.x = 500 - ball.radius;
    ball.vx = -ball.vx;
    }

    if(ball.y>500-ball.radius){
    ball.y = 500 - ball.radius;
    ball.vy = -ball.vy;
    }

    }


    //$(document).ready(function() {


    function animate () {
    context.clearRect(0, 0, canvasWidth, canvasHeight);
    for(var i=0; i<balls.length; i++){
    balls[i].draw(context);
    balls[i].x+=balls[i].vx;
    balls[i].y+=balls[i].vy;
    checkBoundaryCollision(balls[i]);
    }
    window.requestAnimationFrame(animate, canvas);
    //movement code goes in here
    };
    window.requestAnimationFrame(animate, canvas);
    });

  • #2
    New Coder
    Join Date
    Oct 2008
    Posts
    48
    Thanks
    6
    Thanked 2 Times in 2 Posts
    To use the code you have to remove //// and close the script

    </script>
    But didn't work in any browser i tested


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •