I would like assistance in fixing my code

 /* Constants */ var START_RADIUS = 1; var INCREMENT = 1; var CHANGE_COLORS_AT = 10; var circle; function start(){ //Circle is being added once in the start function. circle = new Circle(START_RADIUS); circle.setPosition(getWidth()/2, getHeight()/2); add(circle); //This is the command that will execute every 50 miliseconds. setTimer(grow, 50); } function grow(){ //This will keep the circle from continually growing past the height of the (premade) canvas. while(circle.getRadius()*2 != getHeight()){ START_RADIUS = START_RADIUS + INCREMENT; circle.setRadius(START_RADIUS); //Changes the color every +10 the radius grows. if(circle.getRadius() % CHANGE_COLORS_AT == 0){ circle.setColor(Randomizer.nextColor()); } } } 

This code is meant to make a continually growing circle (until the diameter hits the top of the canvas). This is for school, and is using a very simplified version of javascript from the website 'codehs.com'. I have been working on this code for a while, and would like some insight on how to fix it.

3

1 Answer

Actually fixed it. The problem was, there was one "while" loop, and the "setTimer" command, which also more or less acts as a while loop. This made the circle instantly inflate to full size. The fixed code is here!VV

/* Constants */ var START_RADIUS = 1; var INCREMENT = 1; var CHANGE_COLORS_AT = 10; var circle; function start(){ //Circle is being added once in the start function. circle = new Circle(START_RADIUS); circle.setPosition(getWidth()/2, getHeight()/2); add(circle); //This is the command that will execute every 50 miliseconds. setTimer(grow, 5); } function grow(){ START_RADIUS = START_RADIUS + INCREMENT; circle.setRadius(START_RADIUS); if(circle.getRadius() % CHANGE_COLORS_AT == 0){ circle.setColor(Randomizer.nextColor()); } } 
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like