Created
June 19, 2019 20:03
-
-
Save otarza/c22110438cdc9bbab64a925f208418b0 to your computer and use it in GitHub Desktop.
CodeHS Click for collision.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var RADIUS = 25; | |
var DX_RED = 6; | |
var DX_BLUE = 4; | |
var DELAY = 40; | |
var blueBall; | |
var redBall; | |
var colided = false; | |
var stopped = true; | |
function start(){ | |
blueBall = drawBall(RADIUS, Color.blue, 50, getHeight() / 2); | |
redBall = drawBall(RADIUS, Color.red, getWidth() / 2, getHeight() / 2); | |
mouseClickMethod(click); | |
} | |
function drawBall(radius, color, x, y){ | |
var ball = new Circle(radius); | |
ball.setPosition(x, y); | |
ball.setColor(color); | |
add(ball); | |
return ball; | |
} | |
function click(e){ | |
if ( stopped ) { | |
setTimer(moveBalls, DELAY); | |
stopped = false; | |
} else { | |
stopTimer(moveBalls); | |
stopped = true; | |
} | |
} | |
//moves blue ball | |
function moveBalls(){ | |
if (blueBall.getX() >= (redBall.getX() - RADIUS - RADIUS)) { | |
colided = true; | |
} | |
if (colided) { | |
blueBall.move(DX_BLUE, 0); | |
redBall.move(DX_RED, 0); | |
} | |
else { | |
blueBall.move(DX_BLUE, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
var blueCircle;
var redCircle;
var RADIUS = 25;
var DX_RED = 6;
var DX_BLUE = 4;
var DELAY = 40;
var clicks = 0;
function start(){
blueCircle = new Circle(RADIUS);
blueCircle.setPosition(50, getHeight()/2);
blueCircle.setColor(Color.blue);
add(blueCircle);
}
function click(){
if(clicks%2==0){
setTimer(moveBall, DELAY);
clicks++;
}else{
stopTimer(moveBall);
clicks++;
}
}
function moveBall(){
if (blueCircle.getX() >= (getWidth()/2 - RADIUS*2)){
blueCircle.move(DX_BLUE, 0);
redCircle.move(DX_RED, 0);
}else{
blueCircle.move(DX_BLUE, 0);
}
}