Skip to main content
added 150 characters in body
Source Link
RoToRa
  • 11.6k
  • 1
  • 24
  • 51

I don't see any problem when I move the two lines you mentioned to an earlier position (directly after the check for canvas support).

  • You need to clean up the indention.

  • Use one var statement with the variables separater with commas instead of multiple statements.

  • No need to prefix all those variables with temp.... Also declare them inside the loop. That makes it clear that you only are using them in there.

  • Your code to generate random integers between two limits is wrong. tempRadius will be between 5 and 12, but the maxSize is 8.

  • tempX/tempY calculation adds 2*tempRadius, but then you subtract it again, so it has no effect. This is basiclly the same mistake as with the calculation of the radius (see next point). Also it seems to be the attempt to have the balls bounce when their size touch the walls instead of their center, but you fail to include this when calculating the bouncing.

  • Considering both your attempts to get a random integer between to limits are wrong, you should write a simple function that does that (correctly). Test it.

  • Apropos bouncing: The balls don't bounce off the walls any way. Currently they bounce when they happen to have passed the wall.

  • There is no need to floor tempAngle.

  • You can simplify the calculation of tempRadians to tempRadians = Math.random() * 2 * Math.PI .

  • There is no need to recalculate xunits/yunits of the ball from the angle. When a ball bounces off the left or right walls, then ball.xunits simply becomes -ball.xunits. (Equivalently the top and bottom walls and yunit)

  • You should use a module pattern to wrap your code, so you don't need to use global variables (`context´).

EDIT: Here's my version: http://jsfiddle.net/jjM3V/ (Not quite perfect, as it has some duplicate code in the bounce calculation, that I don't like).

I don't see any problem when I move the two lines you mentioned to an earlier position (directly after the check for canvas support).

  • You need to clean up the indention.

  • Use one var statement with the variables separater with commas instead of multiple statements.

  • No need to prefix all those variables with temp.... Also declare them inside the loop. That makes it clear that you only are using them in there.

  • Your code to generate random integers between two limits is wrong. tempRadius will be between 5 and 12, but the maxSize is 8.

  • tempX/tempY calculation adds 2*tempRadius, but then you subtract it again, so it has no effect. This is basiclly the same mistake as with the calculation of the radius (see next point). Also it seems to be the attempt to have the balls bounce when their size touch the walls instead of their center, but you fail to include this when calculating the bouncing.

  • Considering both your attempts to get a random integer between to limits are wrong, you should write a simple function that does that (correctly). Test it.

  • Apropos bouncing: The balls don't bounce off the walls any way. Currently they bounce when they happen to have passed the wall.

  • There is no need to floor tempAngle.

  • You can simplify the calculation of tempRadians to tempRadians = Math.random() * 2 * Math.PI .

  • There is no need to recalculate xunits/yunits of the ball from the angle. When a ball bounces off the left or right walls, then ball.xunits simply becomes -ball.xunits. (Equivalently the top and bottom walls and yunit)

  • You should use a module pattern to wrap your code, so you don't need to use global variables (`context´).

I don't see any problem when I move the two lines you mentioned to an earlier position (directly after the check for canvas support).

  • You need to clean up the indention.

  • Use one var statement with the variables separater with commas instead of multiple statements.

  • No need to prefix all those variables with temp.... Also declare them inside the loop. That makes it clear that you only are using them in there.

  • Your code to generate random integers between two limits is wrong. tempRadius will be between 5 and 12, but the maxSize is 8.

  • tempX/tempY calculation adds 2*tempRadius, but then you subtract it again, so it has no effect. This is basiclly the same mistake as with the calculation of the radius (see next point). Also it seems to be the attempt to have the balls bounce when their size touch the walls instead of their center, but you fail to include this when calculating the bouncing.

  • Considering both your attempts to get a random integer between to limits are wrong, you should write a simple function that does that (correctly). Test it.

  • Apropos bouncing: The balls don't bounce off the walls any way. Currently they bounce when they happen to have passed the wall.

  • There is no need to floor tempAngle.

  • You can simplify the calculation of tempRadians to tempRadians = Math.random() * 2 * Math.PI .

  • There is no need to recalculate xunits/yunits of the ball from the angle. When a ball bounces off the left or right walls, then ball.xunits simply becomes -ball.xunits. (Equivalently the top and bottom walls and yunit)

  • You should use a module pattern to wrap your code, so you don't need to use global variables (`context´).

EDIT: Here's my version: http://jsfiddle.net/jjM3V/ (Not quite perfect, as it has some duplicate code in the bounce calculation, that I don't like).

Source Link
RoToRa
  • 11.6k
  • 1
  • 24
  • 51

I don't see any problem when I move the two lines you mentioned to an earlier position (directly after the check for canvas support).

  • You need to clean up the indention.

  • Use one var statement with the variables separater with commas instead of multiple statements.

  • No need to prefix all those variables with temp.... Also declare them inside the loop. That makes it clear that you only are using them in there.

  • Your code to generate random integers between two limits is wrong. tempRadius will be between 5 and 12, but the maxSize is 8.

  • tempX/tempY calculation adds 2*tempRadius, but then you subtract it again, so it has no effect. This is basiclly the same mistake as with the calculation of the radius (see next point). Also it seems to be the attempt to have the balls bounce when their size touch the walls instead of their center, but you fail to include this when calculating the bouncing.

  • Considering both your attempts to get a random integer between to limits are wrong, you should write a simple function that does that (correctly). Test it.

  • Apropos bouncing: The balls don't bounce off the walls any way. Currently they bounce when they happen to have passed the wall.

  • There is no need to floor tempAngle.

  • You can simplify the calculation of tempRadians to tempRadians = Math.random() * 2 * Math.PI .

  • There is no need to recalculate xunits/yunits of the ball from the angle. When a ball bounces off the left or right walls, then ball.xunits simply becomes -ball.xunits. (Equivalently the top and bottom walls and yunit)

  • You should use a module pattern to wrap your code, so you don't need to use global variables (`context´).