Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var ball;
- var ball2;
- var dx = 4;
- var dy = 4;
- function start(){
- ball = new Circle(20);
- ball.setPosition(100, 100);
- add(ball);
- setTimer(draw, 20);
- ball2 = new Circle(30);
- ball2.setPosition(105, 105);
- add(ball2);
- setTimer(draw, 20);
- }
- function draw(){
- checkWalls();
- ball.move(dx, dy);
- }
- function draw(){
- checkWalls();
- ball2.move(dx, dy);
- }
- function checkWalls(){
- // Bounce off right wall
- if(ball.getX() + ball.getRadius() > getWidth()){
- dx = -dx;
- }
- // Bounce off left wall
- if(ball.getX() - ball.getRadius() < 0){
- dx = -dx;
- }
- // Bounce off bottom wall
- if(ball.getY() + ball.getRadius() > getHeight()){
- dy = -dy;
- }
- // Bounce off top wall
- if(ball.getY() - ball.getRadius() < 0){
- dy = -dy;
- }
- function checkWalls(){
- // Bounce off right wall
- if(ball2.getX() + ball2.getRadius() > getWidth()){
- dx = -dx;
- }
- // Bounce off left wall
- if(ball2.getX() - ball2.getRadius() < 0){
- dx = -dx;
- }
- // Bounce off bottom wall
- if(ball2.getY() + ball2.getRadius() > getHeight()){
- dy = -dy;
- }
- // Bounce off top wall
- if(ball2.getY() - ball2.getRadius() < 0){
- dy = -dy;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement