2017-02-03 39 views
-3
/* 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()); 
     } 
    } 
} 

这段代码是为了让一个不断增长的圆圈(直到直径达到画布的顶部)。这是针对学校的,并且正在使用来自网站'codehs.com'的javascript的非常简化的版本。我一直在研究这段代码,并希望得到一些关于如何解决它的见解。我想帮助我修复我的代码

+0

getHeight'和其他getter如何定义? – Teemu

+1

JavaScript!= Java –

+0

它有什么问题? –

回答

2

其实修复了它。问题是,有一个“while”循环,“setTimer”命令,它也或多或少的作为while循环。这使得圆圈立即膨胀到全尺寸。固定代码在这里!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()); 
    } 
} 
+0

感谢您更新我们:) –