2017-02-03 46 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

感謝您更新我們:) –